Sign up ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

I'm trying to make http://$ transparently rewrite to$SOMETHING.

The config below is giving me a 404 and I'm not sure why.

ServerAlias *

RewriteEngine On
RewriteCond %{HTTP_HOST} (.*)
RewriteRule ^/(.*) /app/%1/$1

I'm missing something obvious, what is it?

Edit: let me make this a little clearer.

If I browse to, I get a page. If I browse to I expect to see the same page, but I'm getting a 404 instead.

Edit2: The DNS record for * is already in place and I can see my request in the access log, with the 404 status.

Edit3: After enabling the mod_rewrite log file, it appears that the post-rewrite path is "$DOCROOT/app/abc" which fails because it doesn't exist. This is a Python/WSGI application, I think that has something to do with my problem. For the moment it looks like we'll just use a regular redirect instead.

share|improve this question

migrated from Apr 8 '11 at 13:23

This question came from our site for system and network administrators.

3 Answers 3

You need to set a DNS-record for * to redirect to the IP of the server. I think in your case that will be the same as the one pointing to.

Edit: I think one of these should do the trick (normally both):

RewriteCond %{HTTP_HOST} !^app\.example\.com
RewriteCond %{HTTP_HOST} ^([^.]+)
RewriteRule ^(.*) /app/%1/$1 [L]

RewriteCond %{HTTP_HOST} ^([^.]+)\.app\.example\.com$
RewriteRule ^/(.*)$ $1 [L,R]
share|improve this answer
D'oh! Sorry, I should've said that's already in place. Updated the question. –  Graeme Donaldson Apr 8 '11 at 9:12
No problem :-), edited my post with my take on it. –  TiZon Apr 8 '11 at 9:36
That generates an external redirect, which I don't want. –  Graeme Donaldson Apr 8 '11 at 9:40

Perhaps something like:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} !^example\.com
RewriteCond %{HTTP_HOST} ^(.)+\.example\.com
RewriteCond %{REQUEST_URI} !^/app/$1
RewriteRule ^(.*)$ app/$1/$2 [L]

Which ensures you're not currently accessing or directly; then takes your prefix, checks that you're not currently accessing at the /app directly (or it will loop forever), and then rewrites. This should be transparent..

Elaborated from

share|improve this answer

We ended up using an external redirect instead.

RewriteCond %{HTTP_HOST} (.*)
RewriteRule ^/(.*)$1 [R]
share|improve this answer

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.