homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

301 Redirect Issue

 6:20 pm on Aug 26, 2010 (gmt 0)

I am trying to permanently redirect one url to another, but the url doesn't change, and the access log displays a 200. I tried the following:

RewriteRule ^events\.php?id=22&rm=1&a=check$ /events/meet-22/room-1/check [R=301,L]


RedirectMatch 301 ^/events\.php?id=22&rm=1&a=check$ /events/meet-22/room-1/check

What do I need to do to get the url to change over? Thanks.



 6:41 pm on Aug 26, 2010 (gmt 0)

Add the protocol and domain name to the RewriteRule target, as well as the [L] flag.

Remove the attached query string parameters from the RewriteRule pattern and instead test the QUERY_STRING value using a preceding RewriteCond.

Clear your browser cache before testing.


 7:19 pm on Aug 26, 2010 (gmt 0)

Thanks, I tried this:

RewriteCond %{QUERY_STRING} ^id=22&rm=1&a=check$
RewriteRule ^events\.php$ http://www.example.com/events/meet-22/room-1/check [R=301,L]

and now I get an error, "Firefox has detected that the server is redirecting the request for this address in a way that will never complete."

Does this have to do with protocol? Not sure about that, thanks.


 8:13 pm on Aug 26, 2010 (gmt 0)

Clear the query string by appending a "?" to the substitution URL:

RewriteCond %{QUERY_STRING} ^id=22&rm=1&a=check$
RewriteRule ^events\.php$ http://www.example.com/events/meet-22/room-1/check? [R=301,L]

This prevents an 'infinite' loop in the case where events.php is defined your DirectoryIndex page, either using the DirectoryIndex directive itself or by use of another mod_rewrite rule.



 9:26 pm on Aug 26, 2010 (gmt 0)

I added a ? to the end but it's still looping:

RewriteCond %{QUERY_STRING} ^id=22&rm=1&a=check$
RewriteRule ^events\.php$ http://www.example.com/events/meet-22/room-1/check? [R=301,L]

I also have the following rewrite rule after:

RewriteRule ^events/meet-([0-9]{1,4})/room-([0-9]{1,2})/check$ /events.php?id=$1&rm=$2&a=check [NC]

Any idea how I can stop the looping while still doing the 301 redirect? Thanks.


 4:07 am on Aug 27, 2010 (gmt 0)

I also changed the order so it's like this:

RewriteRule ^events/meet-([0-9]{1,4})/room-([0-9]{1,2})/check$ /events.php?id=$1&rm=$2&a=check [NC]

RewriteCond %{QUERY_STRING} ^id=22&rm=1&a=check$
RewriteRule ^events\.php$ http://www.example.com/events/meet-22/room-1/check? [R=301,L]

and the same looping problem happens. Does anyone know if there's anything else I can try?


 7:40 am on Aug 27, 2010 (gmt 0)

Add the [L] flag to the other rule. Every rule needs the [L] flag.

It loops because the internal path generated by the rewrite matches the URL path the redirect is testing for.

Change the code so that:
1. The external redirect is listed before the internal rewrite.
2. Instead of testing QUERY_STRING, test THE_REQUEST so that the redirect can only happen if the query string is part of an external URL request coming from a user. There are tens of thousands of examples showing that code in this forum.

If you had mentioned in your first question that you also have an internal rewrite, that solution would have been suggested first.


 12:37 pm on Aug 27, 2010 (gmt 0)

Yes, when posting about mod_rewrite, it's always a good idea to fully-specify the problem, and to include all of the code that can interact with the URLs and filepaths being discussed. Since the redirected-to URLs are being rewritten later, the code above cannot work.

The full solution, now that the problem is defined, is:

# Externally redirect direct client requests for script filepath+query to new 'friendly' URL
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /events\.php\?id=([0-9]{1,4})&rm=([0-9]{1,2})&a=check\ HTTP/
RewriteRule ^events\.php$ http://www.example.com/events/meet-%1/room-%2/check? [R=301,L]
# Internally rewrite requests for 'friendly' static-looking URLs to script filepath+query string
RewriteRule ^events/meet-([0-9]{1,4})/room-([0-9]{1,2})/check$ /events.php?id=$1&rm=$2&a=check [NC,L]



 4:11 pm on Aug 27, 2010 (gmt 0)

I apologize for not giving full details about the problem. Anyway, it works great now, thank you very much!

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved