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

I have a site that has dynamic virtual subdomains using mod_rewrite, as defined like this:

<VirtualHost *:80>
    ServerName    example.com
    ServerAlias    *.example.com

    DocumentRoot    /var/www/example.com/www

    RewriteEngine    on
    RewriteCond    %{HTTP_HOST}    ^[^.]+\.examle.com$
    RewriteRule    ^(.+)            %{HTTP_HOST}$1    [C]

    RewriteRule    ^([^.]+)\.example.com(.*)    /var/www/example.com/$1$2


The problem is that I want a specific url, say subdomain.example.com/CONTROL/ to point back to www.example.com/ using a proxy (not url redirecting).

I have tried adding:

RewriteRule ^([^.]+)\.example.com/CONTROL(.*)    /var/www/example.com/www$2 [P]

But that didn't work. Any ideas?

share|improve this question

3 Answers 3

From the RewriteRule docs section on [P]:

You must make sure that the substitution string is a valid URI (typically starting with http://hostname) which can be handled by the Apache proxy module.


RewriteEngine on
RewriteCond   %{HTTP_HOST}              ^subdomain.examle.com$
# [L] means stop processing after this rule
RewriteRule   ^/CONTROL/(.*)            http://www.example.com/$1 [P,L]

RewriteCond   %{HTTP_HOST}              ^[^.]+\.examle.com$
RewriteRule   ^(.+)                      %{HTTP_HOST}$1

RewriteRule   ^([^.]+)\.example.com(.*) /var/www/example.com/$1$2

Make sure you have the proxy modules loaded. If you have trouble enable verbose logging with:

RewriteLog      /path/to/rewrite.log
RewriteLogLevel 9
share|improve this answer
The thing is, I don't just want subdomain.example.com/CONTROL, I also want all my other virtual subdomains to have the same feature, e.g. subdomain2.example.com/CONTROL. So I did this: RewriteEngine on RewriteCond %{HTTP_HOST} ^[^.]+\.example.com$ RewriteRule ^(.+) %{HTTP_HOST}$1 RewriteRule ^/CONTROL/(.*) example.com/$1 [P,L] RewriteRule ^([^.]+)\.example.com(.*) /var/www/example.com/$1$2 The log says it's applying the patter /CONTROL(.*) to the url, but it never maps to the primary domain, treating CONTROL as a directory –  Tony G. Sep 6 '09 at 19:01
Can you show us the rewrite log for one request? Instead of adding comments please add the information (including the above) to your question. –  markdrayton Sep 6 '09 at 19:24

You need to have a RewriteCond for the HTTP_HOST

the RewriteRule's are applied to the URL path only!

share|improve this answer
I disagree. I have this already, which helps to properly map the hostname to a directory. RewriteCond %{HTTP_HOST} ^[^.]+\.examle.com$ RewriteRule ^(.+) %{HTTP_HOST}$1 [C] The problem I'm having is almost the opposite one. Conceptually I want to undo the rewrite rules when a specific path is passed after the host name. –  Tony G. Sep 6 '09 at 19:23

Try this:

RewriteCond    %{HTTP_HOST}    ^subdomain.examle.com$
RewriteRule subdomain.example.com/CONTROL/(.*) http://www.example.com/$1 [P]
share|improve this answer
When I try this, the mod_rewrite works, mapping subdomain.example.com/CONTROL to example.com via proxy, but I get 403 FORBIDDEN in the browser, which I do not get when I go directly to example.com –  Tony G. Sep 6 '09 at 18:54
k, I got it. I had to modify my mod_proxy settings to allow from localhost. Now I no longer get the 403 FORBIDDEN and the site loads properly (with broken links of course, but that can be fixed with more rewrite rules I think) –  Tony G. Sep 6 '09 at 19:47

Your Answer


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