Preventing redirect loop with .htaccess parameter re-write

.htaccessredirect

Let me start by saying I am not an .htaccess or Apache expert by any means. But I have the need to append a hashed term to the end of a URL query.

For example:

http://www.example.com/?query_string=stuff

becomes

http://www.example.com/?query_string=stuff#otherstuff

I am using the following rule (entire .htaccess file):

RewriteCond %{QUERY_STRING} ppw_confirm=(.*)
RewriteRule ^/?(.*) http://www.example.com/?ppw_confirm=%1#go [NE,L]

THE PROBLEM

After adding #go to the end of the parameter string, the .htaccess file still reads the ?ppw_confirm= query. It then attempts to perform a second redirect after which an infinite loop ensues.

I've been searching around for hours with no luck. Basically, I need a way to prevent .htaccess from re-writing twice to thus prevent this endless loop.

Open to any and all suggestions 🙂

Any help is greatly appreciated. Thanks in advance!

Best Answer

The #go part isn't something you can work with in rewrite rules at all; it's client-side, not sent to the server when making a request for that resource, so you're not able to make decisions in rules based on it.

Maybe as part of the redirect, add an additional variable to the query string, then look for that variable and prevent the redirect when it's present?

RewriteCond %{QUERY_STRING} ppw_confirm=(.*)
RewriteCond %{QUERY_STRING} !r=1
RewriteRule ^/?(.*) http://www.example.com/?ppw_confirm=%1&r=1#go [NE,L]