Reputation: 45
I have a website that redirects to a page with a querystring. I need to make that url SEO friendly. The url is
http://www.tasteofkochi.com/dine-detail.php?a=51-fort-kochi-mattanchery
I need it to look like
http://www.tasteofkochi.com/dine-detail/51-fort-kochi-mattanchery
I want to remove the '.php' extension and then replace '?a=' to '/'
Can anybody please let me know how to achieve it.
Upvotes: 0
Views: 244
Reputation:
Sure, you can do it like this:
RewriteEngine on
# 301 redirect any requests for /dine-detail.php?a=xx to /dine-detail/xx
RewriteCond %{QUERY_STRING} (?:^|&)a=([^&]+)(?:&|$)
RewriteRule ^dine-detail\.php$ /dine-detail/%1? [R=301,END]
# Rewrite requests for /dine-detail/xx to /dine-detail.php?a=xx
RewriteRule ^dine-detail/(.+)$ dine-detail.php?a=$1 [QSA,END]
This does two things. Any requests for the old style URLs get 301 redirected to the new ones (an external 301 redirect, so the URL changes in the browser), and the new ones get silently rewritten to work with the script as if they had never changed (an internal rewrite).
It will only work for the dine-detail.php page. Let me know if you want it to be more generic and work for other pages, but this is what you asked for.
You can use query strings on the new /dine-detail/xxx URLs and the a= will get appended to them. Any query string params on the old URLs, other than a=xxx, will be dropped by the redirect.
It needs to go in a .htaccess
file in the root of your website or in a <Directory>
block for the website document root in the main server config.
Upvotes: 2