appreciate help in advance. I'm using mod rewrite and need help in converting specific PHP URLS into prettier URLs.
Specifically, I want to convert the PHP URL:
www.sample.com/subcategory.php?subcategory=sports-achievement&page=1
to
www.sample.com/sports/sports-achievement.html
for the user.
So to be specific, the rule is:
a) the first part of the subategory before the dash should be the
directory (e.g., sports- - into /sports/
b) the entire subcategory as
the page string "sports-achievement.html
c) the PHP pages still needs
to pass the page strings (as there are multiple pages)
Thanks in advance for your help!
To get the the redirect and internal rewrite, you'll have to do something like this.
#Redirect
RewriteCond %{THE_REQUEST} (GET|POST)\ /subcategory\.php\?subcategory=([^-]+)-([^&]+)(&(.*)|)\ HTTP
RewriteRule ^subcategory\.php$ /%2/%2-%3.html?%5 [R,L]
#Internal rewrite
RewriteRule ^(.*)/\1-(.*)\.html$ subcategory.php?subcategory=$1-$2 [QSA,L]
There is no real way of hiding the page name. You can use a post-request and hide it in the post-body. My answer to this question shows how to do it with cookies, but this will most likely confuse search engines, as different pages are somehow available under the same page name.
Related
My site is:
example.com
My mobile version:
m.example.com
Profile page is
example.com/profile or m.example.com/profile
Or posting page
example.com/posts or m.example.com/posts
Mobile version's url is similar. So, how to redirect the same page?
For example: user go (from facebook) into example.com/posts but he uses mobile device,
so how to redirect via .htaccess to m.example.com/posts
I saw this one threat, but i'm confuse to create right rules.
Thanks :)
Your question is similar to this question, except that you want to redirect the user. First you need a list of mobile user-agents. I am not going to suggest a list, but I think you should be able to find a list that is suitable for you.
Next, you want to go to your .htaccess file and add something like this:
RewriteCond %{HTTP_USER_AGENT} ^(user-agent1|user-agent2|user-agent3|etc)$
RewriteCond %{HTTP_HOST} !^m\.example\.com$
RewriteRule ^(.*)$ http://m.example.com/$1 [R,L]
The first RewriteCond contains all user-agents you want to redirect. Remember that it is a regex, so you have to escape any special characters! The second RewriteCond checks if the host isn't already m.example.com. If we wouldn't check for this, we would create an infinite loop, redirecting to the same url over and over. The RewriteRule itself matches everything after the hostname (and before the query string) and redirects it to the mobile site. $1 gets replaced with the first capture group in the regex in the first argument of RewriteRule. The [R] flag is not necessary in this case (links with http:// will always be redirected), but it let's Apache know that it should do an external temporary redirect to the new url. The [L] flag will stop processing more rules if this rule matches.
See the documentation for more information.
I think my question is quite simple but I've been banging my head against the wall for the past few hours.
I have my website using rewriterule to ensure messy path names with php variables are now nice and tidy(I've removed [http://www] from my examples because the system thinks I am putting links and won't let me).
So somebody comes to my site at mysite.co.uk/my-product-P1.html the website will know to post mysite.co.uk/product.php?id=1 to the server.
But I also want to tidy it up the other way around. If an old customer or an old link uses the pathname mysite.co.uk/product.php?id=1 then I want it to return mysite.co.uk/my-product-P1.html instead even though the old pathname will actually still work. I don't want customers accessing the same page from different pathnames.
How do I do this and will it create a loop? On another website I have it working using:
RewriteCond %{QUERY_STRING} ^id=1$ [NC]
RewriteRule ^product\.php$ product-P1.html? [R=301,L]
But on that site there are only around 10 products so I'm able to write these lines for each products. On my other site I have hundreds of products so this isn't practical and I need to do it automatically.
Hopefully this makes sense. I have read through other posts and can't find my solution so apologies if this is clearly explained somewhere else.
How do I do this and will it create a loop?
The rules that you have (on the site with 10 products) need to match against the actual request as opposed to the URI:
RewriteCond %{THE_REQUEST} \ /product\.php\?id=([^\ &]+)
RewriteRule ^ /product-P%1.html? [R=301,L]
But you're better off doing this in your php script rather than enumerating all the products in the htaccess file:
On my other site I have hundreds of products so this isn't practical and I need to do it automatically
You can't do that using only mod_rewrite. You'll need to script that in your product.php script. The product.php script will need to check the $_SERVER'[REQUEST_URI'] php variable, and see if it starts with: /product.php.
If it does, then you know someone accessed the php script directly, and you'll need to fetch the product name using the id passed in $_GET['id'], then redirect to the product name + "-P$_GET['id'].html".
The htaccess file and mod_rewrite won't know the mapping between product IDs and product names, so you need to do this in your php script (which does have access to this mapping).
You need this additional rule:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+product\.php\?id=([^\s&]+) [NC]
RewriteRule ^ /product-P%1.html? [R=302,L]
I have a website developed in PHP. I have recently done the URL rewriting which works fine. However, I just found out that my pages with parameters are also accessible. For eg.
I converted this URL
domainname.com/index.php?page=product&pid=5&proTitle=Samsung Galaxy
After rewrite it looks like this
domainname.com/products/5/Samsung-Galaxy.html
Everything works just fine. However, My site is still accessible using the old parameters. I want if someone types in the old URL should be automatically redirected to Ideally New Page if not then index page. Google and MSN shouldn't access these pages with parameter. Any help will be highly appreciated.
Thanks for your input. Here is more detail to my question.
I converted this URL
domainname.com/index.php?page=product&pid=5&proTitle=Samsung Galaxy
After rewrite it looks like this
domainname.com/products/5/Samsung-Galaxy.html
The code looks like this.
RewriteRule ^products/(.*)/(.*).html$ index.php?page=product&pid=$1&proTitle=$2 [nc]
Rewrite Works fine. However, if I try to access old URL i.e domainname.com/index.php?page=product&pid=5&proTitle=Samsung Galaxy the page is still accessible and on top of that being crawled by Google and other search engines. I want If someone tries to access this URL, it should direct them to Page Not Found and this should also not be sniffed by any crawlers.
Thanks a lot again for your time and I hope I can get your valuable reply soon.
You need another rule which redirects the browser to the nicer looking URL if the request is made for the ugly looking one. For example:
# need to replace spaces with "-"
RewriteRule ^(.*)\ (.*)$ /$1-$2 [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?page=([^&\ ]+)&pid=([^&\ ]+)&proTitle=([^&\ ]+)
RewriteRule ^ /%1/%2/%3 [L,R=301]
I have a website and i am using MySQL to store and fetch data from, there is a bunch of data of different destinations (Yes this is a travel agent website) i am wondering how can i setup .htaccess file to display SEO friendly URL
For example: http://www.mywebsite.com/flights-result.php?id=10 this URL is a details page for a flight to Entebbe in Africa, i would like to have the URL for this like http://www.mywebsite.com/Africa/Entebbe.htm
And so on for them, one more thing do i need to add this for every page? the data is being update on daily basis so is there any easy way to write URL automatically?
Any help highly appreciated.
I don't really think what you are trying to accomplish has anything to do with mysql. What you are looking for is called URL rewriting. There are countless number of articles out there that could show you the direction to follow. I am not very sure which web server you are using right not. I presume it is Apache. Here is Apache module_rewrite guide.
Given the original URL, there isn't all the information in there to use mod_rewrite to do this completely.
So what you could do it send all web requests to a controller file, and from there parse the request uri and load the correct page.
So in htaccess, something like...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ controller.php [L]
Then in controller.php you parse the url and load the correct page.
A different option you may prefer, (if you're flexible on the specific final URL) is to have URLs ending up looking like this:
http://www.mywebsite.com/flights/10/Africa/Entebbe.htm
This would likely be simpler to do instead of implementing a controller (although I prefer the controller for routing requests).
So in htaccess...
RewriteRule
^/flights/([0-9]{1,10})/([a-zA-Z]+)/([a-zA-Z]+)\.htm$
flights-result.php?id=$1&country=$2&place=$3 [L]
Then near the start of the flights-results.php file you should load the data for the id, then check that the provided "country" and "place" are correct (to stop people just entering anything here), and return a 4040 if it's not.
Remember to change all the links your app outputs to the new style as well.
You could also, as you mentioned, hard code all these URLs into a htaccess, but that's not ideal :)
I have some PHP and HTML in the same file, and I am not exactly sure how to make the URL appear as the name of that page.
Here is an example of what I would like to do: lets say some page id = 1 and the title of that page is HelloWorld.
Instead of site.com/page.php?id=1 I would like the url to appear as site.com/HelloWorld
But the problem I am having is that I only get to know the title of the page inside that page after I query for the title by id.
Considering the setup I described, is there a way to make the urls appear as the names of the pages? And also, if someone links to that page by using the better looking url with the name of the page instead of the id, is there still a way to get the id and by that, the rest of the page contents?
Thanks!!
What you need is to learn more on how .htaccess work.
Here is a good link that got me started:
.htaccess tricks and tips
Update:
Here is a very common practice in many framework where all requests are sent to index.php, and from there you use php to serve the correct page:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://www.site.com/index.php [L,R=301]
</IfModule>
This is actually a function of your HTTP server software (often Apache), not of PHP.
What you're seeing happen on sites with "friendly" URLs is that the friendly name is captured in a Regular Expression and then passed to PHP.
For example:
GET /HelloWorld
is sent to your web server.. the web server parses it
RewriteCond ^(A REGULAR EXPRESSION TO CAPTURE YOUR FRIENDLY NAME)$
RewriteRule page.php?id=(EXPRESSION FROM ABOVE)
In this way your PHP script will always receive the friendly name as a parameter.
Take a look at "mod_write" for Apache - which you can often create rules for using an ".htaccess" file in the root directory.