Forum Moderators: phranque

Message Too Old, No Replies

RedirectMatch to RewriteRule

Trying to Redirect an old link to a RewriteRule

         

aconway

5:34 pm on Sep 13, 2005 (gmt 0)

10+ Year Member



I have some old links that I am trying to redirect to a url that has a RewriteRule associated with it. However, it isn't working. Any help would be appreciated.

RedirectMatch 301 ^/items/(hat¦shirt¦pants)\.php?id=(.+)$ [mysite.com...]

RewriteEngine on
RewriteRule ^(hat¦shirt¦pants)/(hat¦shirt¦pants)_prices/(.+).html$ /items/$1.php?id=$3

jd01

6:09 pm on Sep 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi aconway,

Welcome to WebmasterWorld.

You will need to use mod_rewrite for both or you will create an infinite loop. Currently, your redirect sends uses to the static location, then your rewrite forces Apache to open the .php location, but Apache is redirected to the new location by the redirect, where it tries to open the .php location, but is then reidrected...

The only way to break the loop is using %{THE_REQUEST} from mod_rewrite:

RewriteEngine on
RewriteCond %{THE_REQUEST} \?id=(.+)
RewriteRule ^items/(hat¦shirt¦pants)\.php$ http://www.mysite.com/$1/$1_prices/%1.html [R=301,L]

RewriteRule ^(hat¦shirt¦pants)/(hat¦shirt¦pants)_prices/(.+).html$ /items/$1.php?id=$3 [L]

%{THE_REQUEST} will only redirect original requests, so you will be able to redirect when a URL is clicked on, typed into a browser, or spidered, but will not redirect if the request is from a rewrite.

Hope this helps.

Justin

Still a little tired, so you might find some efficiency somewhere, but this is the general idea.

aconway

6:28 pm on Sep 13, 2005 (gmt 0)

10+ Year Member



Thanks for the response... I tried the code you gave me, but I am getting the following error:

Redirection limit for this URL exceeded. Unable to load the requested page. This may be caused by cookies that are blocked.

And cookies are enabled. So, I'm not sure what went wrong?

aconway

6:50 pm on Sep 13, 2005 (gmt 0)

10+ Year Member



OK I just made a change on this line. I added the $

RewriteCond %{THE_REQUEST} \?id=(.+)$

the page is redirected, but it appears as:

[mysite.com...]

how do I get rid of the?id=(.+)?

jd01

7:48 pm on Sep 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry,

RewriteCond %{THE_REQUEST} \?id=(.+)
RewriteRule ^items/(hat¦shirt¦pants)\.php$ http://www.mysite.com/$1/$1_prices/%1.html? [R=301,L]

Should be better -- Make sure you empty your cache.

Justin

Added: The $ is not at the end of the line...

RewriteCond %{THE_REQUEST} \?id=(.+)\ /HTTP1\.(0¦1)$

Would be the full line ending (or close to it) better to use the implicit 'and everything else' by leaving the anchor off.

aconway

8:11 pm on Sep 13, 2005 (gmt 0)

10+ Year Member



I switched the $ with \/HTTP1\.(0¦1)$

and I am still getting the trailing?id=whatever after .html

aconway

8:18 pm on Sep 13, 2005 (gmt 0)

10+ Year Member



I take that back it works.

THANK YOU VERY MUCH!