Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to introduce nice urls:

RewriteRule ^(.*).html$ index.php?arianne_url=content/$1 [L]

And i want to externally rewrite access to the old style urls:

RewriteCond %{QUERY_STRING} arianne_url=content/([^&]*)
RewriteRule ^.* /%1.html? [R=301]

The problem is that this ends in an endless loop.

I tried to define env and check it, but this still results in an endless loop

RewriteRule ^(.*).html$ index.php?arianne_url=content/$1 [L,env=arianne:rewrite]

RewriteCond %{env:arianne} ^$
RewriteCond %{QUERY_STRING} arianne_url=content/([^&]*)
RewriteRule .* /%1.html? [R=301]

I have to do this in .htaccess as I don't have access to the server configuration files.



# enable nice urls
RewriteRule ^(.*).html$ index.php?arianne_url=content/$1 [L,env=arianne:rewrite]

# Redirect old style request to new nice url
RewriteCond %{env:arianne} ^$
RewriteCond %{QUERY_STRING} arianne_url=content/([^&]*)
RewriteRule ^.* /%1.html? [R=301,L]
share|improve this question
up vote 1 down vote accepted

Have you tried adding 'L' (last) to your RewriteRule? i.e.,

RewriteRule ^.* /%1.html? [R=301,L]

This will mark the rule as being the last rule, and is essentially equivalent to a break statement in C. Once this rule is executed, it will stop matching rules.

Could that be the reason?


share|improve this answer
I have tried it but it is ignored. – Hendrik Brummermann Aug 28 '10 at 15:14
Can you enable and/or append the RewriteLog to your post? That is, enable RewriteLog /path/to/log and RewriteLevel 5 to get ultra verbose. – Andrew M. Aug 28 '10 at 15:24
It keeps applying the rules again and again after each rewrite: new – Hendrik Brummermann Aug 28 '10 at 15:57
The first RewriteRule will match the URL and forward it internally, keeping the browser URL the same. Can you give me the URL the second condition (matching the "old style request") is trying to match? – Andrew M. Aug 28 '10 at 17:58
Than you for the tip with the rewrite log, i now understand what happens. As workaround i add a parameter: rewrite=done to the url in the first block. And check via RewriteCondition that this parameter is not defined before sending the external redirect. – Hendrik Brummermann Aug 31 '10 at 20:56

Apache ENV variables as set in rewrite do not persist over redirects - so once you do the R=301, it's back to square one - the request is working in brand new apache context. That's your infinite loop.

If you want to do this via rewrite (which makes sense - so the browser sees the proper URL) you probably have to set up either a cookie rather than trying to use ENV, or else do some funky stuff with actual server paths behind the scenes, excluding certain ones from redirection.

Also - the [L] as others have said is of no use - it will cause the rest of hte rewriterules to be ignored completely.

share|improve this answer
I think his logic is incorrect; he's having the first rule convert a.html -> index.php?arianne_url=content/a, and the second rule is converting index.php?arianne_url=content/a -> a.html. The last comment on my answer was requesting what he MEANT to do for the second rule. – Andrew M. Aug 29 '10 at 17:18

Your Answer


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.