Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

For business-specific reasons I created the following rewrite rule for Apache 2.2.22 (mod_rewrite):

RewriteRule /site/(\d+)/([^/]+)\.html /site/$2/$1 [R=301,L]

Which if given an URL like: http://www.mydomain.com/site/0999/document.html

Is translated to: http://www.mydomain.com/site/document/0999.html

That's the expected scenario. However, there are documents which name are only numbers. So consider the following case:

http://www.mydomain.com/site/0055/0666.html

Gets translated to:

http://www.mydomain.com/site/0666/0055.html

Which also matches my rewrite rule pattern, so I end up with "The web page resulted in too many redirects" errors from browsers.

I have researched for a long time, and haven't found "good" solutions. Things I tried:

  1. Use the [END] flag. Unfortunately is not available on my Apache version nor it works with redirects.

  2. Use %{ENV:REDIRECT_STATUS} on a RewriteCond clause to end the rewrite process (L). For some reason %{ENV:REDIRECT_STATUS} is empty all the times I tried.

  3. Add a response header with the Header clause if my rule matches and then check for that header (see: here for details). Seems that a) REDIRECT_addHeader is empty b) headers are can't be set on the 301 response explicitly.

There is another alternative. I could set a query parameter to the redirect URL which indicates it comes from a redirect, but I don't like that solution as it seems to hacky.

Is there a way to do exactly what the [END] flag does but in older Apache versions? Such as mine 2.2.22.

Thanks!

share|improve this question

2 Answers 2

up vote 2 down vote accepted

Add a query parameter to the redirect is the best option. It is done quite often for all kinds of reasons.

For example make the rule like this:

RewriteCond %{QUERY_STRING}  !redirected
RewriteRule /site/(\d+)/([^/]+)\.html /site/$2/$1?redirected [R=301,L]

What now happens is that the redirects are to URLs with "?redirected" added to them. And so the rule isn't applied the second time.

share|improve this answer
    
Yes, I think this will be my solution eventually. Thanks! –  Javier Méndez Jun 29 '13 at 20:13

The [END] flag is not relevant here, since you are serving 301 redirects. And as the documentation you linked to states...

This does not apply to new requests resulting from external redirects.

If you really need to do this sort of redirect, then you need to find some other way to disambiguate the old and new URLs.

share|improve this answer
    
Got it. I can't believe I missed that comment on the documentation... I did know the root of the issue was the I'm doing a redirect. Well, thanks. –  Javier Méndez Jun 28 '13 at 16:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.