Forum Moderators: phranque

Message Too Old, No Replies

Mod Rewrite 301 Redirect Problem

         

iridiya

3:14 pm on May 21, 2012 (gmt 0)

10+ Year Member




System: The following message was cut out of thread at: http://www.webmasterworld.com/apache/4455792.htm [webmasterworld.com] by incredibill - 8:43 am on May 21, 2012 (PST -8)


Hi guys, I have been using mod-rewrite for a while and the problem I have now is confusing. I am trying to 301 redirect some old urls to their respective new versions, but it does not work right for some reason. Here is the sample of my htaccess file with the rewrites in question:

RewriteRule ^real-estate/mls.html?searchcity=NORTH+BETHANY&(.*)$ /North_Bethany/Homes/ [R=301,L,NC]
RewriteRule ^real-estate/mls.html?searchcity=SOUTH+BETHANY&(.*)$ /South_Bethany/Homes/ [R=301,L,NC]
... for several cities
RewriteRule ^real-estate/mls.html$ /real-estate-search.html [R=301,L]

The last line is a general redirect when there are no search options. However, all the city searches redirect to that general page as though rules for city searches do not get matched.
As per suggestions above I tried using protocol and domain name as follows:

RewriteRule ^real-estate/mls.html?searchcity=NORTH+BETHANY&(.*)$ http://www.example.com/North_Bethany/Homes/ [R=301,L,NC]

but that did not change the outcome - still not redirecting right.

I would greatly appreciate your suggestions, as I have played with it for a while with no luck.

iridiya

3:42 pm on May 22, 2012 (gmt 0)

10+ Year Member



I solved this, in case it will help someone. The working snippet is

RewriteCond %{QUERY_STRING} ^searchcity=NORTH\+BETHANY(.*)$
RewriteRule ^(.*)$ http://www.example.com/North_Bethany/Homes/? [R=301,L]

g1smd

3:55 pm on May 22, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



In the RewriteCond the (.*)$ part can be removed.

(.*)$ - says "capture everthing to the end and then do nothing with it".

In the RewriteRule, do you really mean (.*) as the pattern? This means that requests for images, stylesheets, js files etc will each be tested for the particular query string. That's a waste of time. Limit the checking only to the particular affected paths and files (e.g. ^real-estate/mls\.html or similar) here.

iridiya

10:26 pm on May 22, 2012 (gmt 0)

10+ Year Member



Thank you. I did not realize that it would check images, javascript, etc. I incorporated your suggestions.

g1smd

10:43 pm on May 22, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The (.*) pattern matches all requests hitting the server.

Limit the work by using a more specific pattern.