2
votes

I have a rewrite rule in Htaccess as below for a dynamic URL

RewriteRule ^cartoon-([^-]*)-([^-]*)\.html$ /fm
/cart_new?r_id=$1&location=$2 [L]

This rule results into URL as http://localhost/fm/cartoon-34-singapore.html

Now my client want to change this URL to http://localhost/fm/singapore/34/goofie and i wrote .htaccess as

RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)/?$ /fm
/cart_new?location=$1&r_id=$2&cartooname=$3 [L]

The above rewrite is working fine but client wants that all OLD URLs like i.e.http://localhost/fm/cartoon-34-singapore.html shall 301 redirect to http://localhost/fm/singapore/34/goofie.

This is driving me crazy. I have tried various things but none seems working.

1
Old URL http://localhost/fm/cartoon-34-singapore.html doesn't have cartooname in it. So it cannot redirect to http://localhost/fm/singapore/34/goofie - anubhava
can we chnage URL somehow in PHP by sending 301 rediredct headers if URL pattern matches? - Gags
Yes it can be done in PHP and then do redirect in PHP itself - anubhava
Can u please add this as an answer so that i can accept same. - Gags

1 Answers

1
votes

In your PHP code you can do something like this for redirection:

header ('HTTP/1.1 301 Moved Permanently');
header("Location: {$rebuilt_url}");

Here $rebuilt_ur will be set to http://localhost/fm/singapore/34/goofie by querying your database.