Search code examples
apachemod-rewriteapache2mod-auth-passthrough

Transparent rewrite/Pass Through of wildcard subdomain


I am close to achieving my goal but I cannot come up with the correct solution to my problem.

I have written the following rules for my wildcard subdomains:

#remove www.
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://domain\.com/$1 [R=301,L]

#rewrite subdomains to /club/<clubname as defined by subdomain>/<whatever was here before>
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/club/%1/$0 [NC,L]

This is desperately close to what I require, ie if I go to http://alpha.domain.com/some/string/here the URL is rewritten to http://domain.com/club/alpha/some/string/here

however

I would like the url in browser to still look like the original url

Many thanks in advance

EDIT: I have tried just adding PT to the final rule but that doesn't work, I get a 400 error

EDIT2: For anyone interested, I abandoned this line of enquiry and instead used php to read the text in the subdomain.


Solution

  • If the www.example.com subdomain uses the same virtual host as the example.com domain, you can just use an internal rewrite by using a relative path:

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

    Otherwise you will need a proxy:

    RewriteCond %{HTTP_HOST} ^(.+)\.example\.com$ [NC]
    RewriteRule ^(.*)$ http://example.com/club/%1/$0 [NC,L,P]