Jump to content


Photo

301 redirect breaks my mod_rewrite rule


  • Please log in to reply
3 replies to this topic

#1 craigj1303

craigj1303

    Member

  • Members
  • PipPip
  • 12 posts

Posted 23 April 2014 - 06:00 PM

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?



#2 requinix

requinix

    Transforming Moderator

  • Moderators
  • 6,238 posts
  • LocationWA

Posted 23 April 2014 - 09:02 PM

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.

#3 craigj1303

craigj1303

    Member

  • Members
  • PipPip
  • 12 posts

Posted 24 April 2014 - 04:29 AM

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?



#4 requinix

requinix

    Transforming Moderator

  • Moderators
  • 6,238 posts
  • LocationWA

Posted 24 April 2014 - 01:22 PM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com