Jump to content

Recommended Posts

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} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteCond %{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} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteCond %{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?

Link to comment
https://forums.phpfreaks.com/topic/287981-301-redirect-breaks-my-mod_rewrite-rule/
Share on other sites

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.

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?

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/$1
because 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.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.