0

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

1 Answer 1

1

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.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.