craigj1303 Posted April 23, 2014 Share Posted April 23, 2014 Hi There I've been working on creating search engine friendly URLS from query strings. I have written some rules using mod_rewrite and the rules are working well and re-directing query string URL's to my new friendly URL's. However, I seem to have a problem when introducing the [R=301] flag. This code gives me my Search Engine friendly URL in the address bar: RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteCond %{REQUEST_URI} !=/favicon.icoRewriteCond %{REQUEST_URI} ^/ranges/RewriteRule ^([-a-zA-Z_0-9]+)/([a-zA-Z_0-9]+)$ /index.php?page=show_range&range_id=$2 [QSA,L] But after I enter the R=301 flag this code gives displays the full index string URL: RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteCond %{REQUEST_URI} !=/favicon.icoRewriteCond %{REQUEST_URI} ^/ranges/RewriteRule ^([-a-zA-Z_0-9]+)/([a-zA-Z_0-9]+)$ /index.php?page=show_range&range_id=$2 [R=301,QSA,L] I am told that R=301 should be implemented to let Google know that the page has moved. What is going wrong? Quote Link to comment https://forums.phpfreaks.com/topic/287981-301-redirect-breaks-my-mod_rewrite-rule/ Share on other sites More sharing options...
requinix Posted April 24, 2014 Share Posted April 24, 2014 You don't want to tell Google the page has moved. As far as it, or anybody else, is concerned the page is actually located at that friendly URL. If anything you would redirect requests for the old index.php URL to the new friendly URL, which is technically optional but a good thing to do for SEO. Quote Link to comment https://forums.phpfreaks.com/topic/287981-301-redirect-breaks-my-mod_rewrite-rule/#findComment-1477141 Share on other sites More sharing options...
craigj1303 Posted April 24, 2014 Author Share Posted April 24, 2014 Hi Requinix, thanks for your reply. Yes, you are correct, perhaps I phrased it incorrectly. Your comment: If anything you would redirect requests for the old index.php URL to the new friendly URL, which is technically optional but a good thing to do for SEO. That is exactly what I want to do for SEO purposes. What line(s) of code would I need to add to my existing code to make requests for the old URL redirect to the new URL using 301 redirect? I am aware of the Redirect 301 rule but need to understand if that is the correct way to go about it with maybe adding some form of regex? Quote Link to comment https://forums.phpfreaks.com/topic/287981-301-redirect-breaks-my-mod_rewrite-rule/#findComment-1477154 Share on other sites More sharing options...
requinix Posted April 24, 2014 Share Posted April 24, 2014 What you do is redirect requests that are for literally the "/index.php..." pattern you're rewriting to. The catch there is the term "literally". RewriteCond %{REQUEST_URI} ^/index.php\? RewriteCond %{REQUEST_URI} [?&]page=show_range(?=&|$) RewriteCond %{REQUEST_URI} [?&]range_id=([a-zA-Z_0-9]+)(?=&|$) RewriteRule ^ /ranges/%1 [L,R=301]Note that you can't simply do RewriteRule index\.php?page=show_range&range_id=([a-zA-Z_0-9]+) ranges/$1because the friendly rewriting you're doing will conflict with that (mod_rewrite will match this rule against the new URL too). Nor should you do RewriteCond %{REQUEST_URI} ^/index\.php?page=show_range&range_id=([a-zA-Z_0-9]+)because the query string might have the parameters in a different order. Quote Link to comment https://forums.phpfreaks.com/topic/287981-301-redirect-breaks-my-mod_rewrite-rule/#findComment-1477199 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.