Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

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 try to use the first part of the fqdn as a rewrite target: =>

RewriteCond %{HTTP_HOST} ^([a-z0-9-]+) [NC]
RewriteRule ^/(.*)$1 [P]

This creates an endless loop. So I need another condition that checks if this request is already rewritten or not:

RewriteCond %{REQUEST_URI} !^/%1

Well this is not working, the documentation says nothing about using backreferences in CondPatterns, most possibly it's not supported.

RewriteCond %{REQUEST_URI} !^/demo

This works perfectly, but isn't dynamic.

share|improve this question
up vote 2 down vote accepted

Solution/Workaround: reserve one hostname part for internal requests:

RewriteCond %{HTTP_HOST} !^ [NC]
RewriteCond %{HTTP_HOST} ^([0-9a-z-]+) [NC]
RewriteRule ^/(.*)$1 [P]
share|improve this answer

If you don't need to process any rules after the rewrite, just add the "L" = "last" option in the square brackets:

RewriteRule ^/(.*)$1 [P,L]

share|improve this answer
Won't help. This is still doing endless loops. RewriteCond %{HTTP_HOST} ^([a-z0-9-]+) [NC] RewriteRule ^/(.*)$1 [P,L] – turingmachine Aug 11 '10 at 9:21

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.