Reputation: 141
Suppose my website is www.example.com
Earlier the urls of my website was like this www.example.com/php/bookdetails.php?bid=33
Last week i changed the urls of my site to this www.example.com/bookdetails/33
and wrote an htacces like this
RewriteRule ^bookdetails/(.*)$ php/bookSearchResult.php?bid=$1
The problem is that there are some external site using my earlier URL structure (www.example.com/php/bookdetails.php?bid=33) and what i want is to redirect it to my new URL structure (www.example.com/bookdetails/33).
I know it can be possible in htaccess, but i have no idea how to do it
Upvotes: 0
Views: 357
Reputation: 4602
RewriteRule ^php/bookdetails\.php\?bid=(\d+)$ bookdetails/$1 [L,R=301]
A PCRE to match the old format, capturing the number, the new URL with the reference to the captured number, Last rule, Redirect with HTTP status 301 (moved permanently)
(Tested it for syntax, but not to ensure that it works exactly. I'm fairly sure that if this isn't it, it should be close.)
Make sure this rule is before the other you mentioned rewriting to the proper php URL, or you'll get a 500 for an infinitely looping rewrite.
The Rewrite Guide and the Advanced Rewrite Guide are invaluable resources for examples.
EDIT:
BTW, this is a duplicate of .htaccess and seo friendly urls, and probably several others, for additional ideas. (I should have looked for a dup first.)
Upvotes: 1