Hi Just a beginner with all this mod rewrite stuff! I've been looking for a solution for days without any luck! I want to redirect the url example.com/catalogue.php?ctn=whatever (the page cached in search engine) TO example.com/catalogue/whatever.html Which is quite easy like so: RewriteRule ^catalogue/([^/]*)\.html$ /catalogue.php?ctn=$1 [L] The problem is you can still go to catalogue.php?ctn=whatever which is also cached in search engine indexes. So I need to redirect - issue a 301 from catalogue.php?ctn=whatever TO catalogue/whatever.html WITHOUT ENDING UP IN A NEVER ENDING LOOP BECAUSE OF THE 1ST RULE HELP
Put something like this at the top of your catalogue.php: $ctn = $_GET["ctn"]; if ($ctn > '') { header("HTTP/1.1 301 Moved Permanently"); header("Location: http://www.yoursite.com/catalogue/" . $ctn . ".html"); exit; } Code (markup):
Thanks for the reply. If I put the above code in my php file it will redirect to the html page right? Will the first rewite rule kick in again and redirect back to the php page and so on....
Should work fine. I basically copied this from an osCommerce site I maintain where I had to do this very same thing. It shouldn't give you any problems, but if it does, there is another way to accomplish it and I can help you code that as well.
Why don't you rename the catalog.php to cataglognew.php and then do something like... RewriteRule ^catalogue.php?ctn=([^/]*)$ /catalogue/$1.html [R=301,L] RewriteRule ^catalogue/([^/]*)\.html$ /cataloguenew.php?ctn=$1 [L]
zak, another way to accomplish this: $url = explode('=',$_SERVER['REQUEST_URI']); $ctn = $url[1]; if ($ctn > "") { header("HTTP/1.1 301 Moved Permanently"); header("Location: http://www.yoursite.com/catalogue/" . $ctn . ".html"); exit; } Code (markup):
OK, got it working and its quite a simple way to do it copy catalogue.php to catalog.php or any other name you like. change your existing rewrite rule to RewriteRule ^catalogue/([^/]*)\.html$ /catalog.php?ctn=$1 [L] Code (markup): then add RewriteCond %{SCRIPT_FILENAME} catalogue\.php RewriteCond %{QUERY_STRING} ctn=([^/]*) RewriteRule ^.*$ http://www.example.co.uk/catalogue/%1.html? [R=301] Code (markup): self explanatory