4

I've read a lot of questions and answers about this on here but none that seem to solve my specific problem.

I want to redirect any subdomain to the subdirectory to match.

So: x.domain.com would go to domain.com/x, and y.domain.com would go to domain.com/y - But I want to do this without the URL in the address bar changing.

Here's what I have so far:

    Options +FollowSymLinks 

    RewriteEngine on

    RewriteBase /

    RewriteCond %{HTTP_HOST} !^(www)\. [NC]

    RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com [NC]

    RewriteRule ^ /%1 [P,L]

But this takes me to a website redirect loop, with an incorrect address in the URL bar where the subdomain still exists.

For example, x.domain.com takes me to x.domain.com/x and I get a redirect loop error.

I'd be grateful if anyone can point me in the right direction! Nothing I change seems to work...

Newnab
  • 811
  • 1
  • 7
  • 7

3 Answers3

9

First of all, make sure that the vhost in the apache configuration is properly configured and all subdomains of domain.com are in the same host configuration (wildcard):

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

You can get the redirect working with the following htaccess configuration:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com
RewriteRule ^(.*)$ http://domain.com/%1/$1 [L,NC,QSA]

Now, if you open asd.domain.com it should redirect you to domain.com/asd. You will still have the problem, that the redirect is visible in the URL address bar. In order to prevent this, enable mod_proxy (and load the submodules) on your server and exchange the "L" flag with the "P" flag:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com
RewriteRule ^(.*)$ http://domain.com/%1/$1 [P,NC,QSA]

If this doesn't work, viewing the vhost configuration and the content of error.log on subdomain calling will be helpful!

References:
.htaccess rewrite subdomain to directory
http://httpd.apache.org/docs/2.2/mod/mod_proxy.html
http://httpd.apache.org/docs/2.2/rewrite/flags.html#flag_p

Community
  • 1
  • 1
meberhard
  • 1,797
  • 1
  • 19
  • 24
  • I tried this, but I'm getting some errors... [Sun May 26 13:02:28 2013] [error] [client 2.126.187.186] Attempt to serve directory: proxy:http://domain.com/x/ [Sun May 26 13:02:29 2013] [error] [client 2.126.187.186] File does not exist: proxy:http://domain.com/x/favicon.ico – Newnab May 26 '13 at 13:01
  • And when I visit the URL x.domain.com I get a 404, which says: The requested URL / was not found on this server. – Newnab May 26 '13 at 13:03
  • did it work before, when you had the flag "L" instead of "P"? I usually get the message "Attempt to serve directory: proxy:domain.com/x", when mod_proxy is not enabled! And just to be sure, you replaced "domain.com" with your real domain? – meberhard May 26 '13 at 13:07
  • Aha! Mod_proxy wasn't enabled, that was it! I googled myself some answers on how to enable it and now it seems to be working! Thanks! I'll tick this. – Newnab May 26 '13 at 13:13
  • 1
    Ahhhh, hang on! Now I'm getting a redirect loop on www.domain.com, where it's trying to go to www.domain.com/www/www/www/www etc! – Newnab May 26 '13 at 13:54
  • OK, I needed to put this line back in: RewriteCond %{HTTP_HOST} !^(www)\. [NC] - Scared me for a second there! – Newnab May 26 '13 at 13:56
  • Worked for me: `RewriteEngine on RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com RewriteRule ^(.*)$ http://domain.com/%1/$1 [P,NC,QSA]` – Ben Sinclair Apr 03 '14 at 00:57
  • This works for me using the L flag, but when using the P flag, I am unable to go to the directory itself but must append the index file to it. For example, when using the L flag I can go to: http://sub.domain.com and it will redirect me to domain.com/sub (and display index.php) But using P: http://sub.domain.com doesn't work but http://sub.domain.com/index.php does work How can I fix this? – Jeff Vdovjak Jan 16 '15 at 01:28
  • You don't need to use `mod_proxy`. See my solution. – Dan Bray Apr 06 '16 at 18:45
7

This can be achieved in .htaccess without mod_proxy provided your server is configured to allow wildcard subdomains. (I achieved that in JustHost by creating a subomain manually named *). Add this to your .htaccess file:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.website\.com$
RewriteCond %{HTTP_HOST} ^(\w+)\.website\.com$
RewriteCond %{REQUEST_URI}:%1 !^/([^/]+)/([^:]*):\1
RewriteRule ^(.*)$ /%1/$1 [QSA]
Dan Bray
  • 7,242
  • 3
  • 52
  • 70
  • Can you please help me out? I would like to redirect john.website.com to app.website.com/sort.php while keeping john.website.com in the address bar – Rotimi Mar 06 '20 at 11:04
0

I named the subdirectories under $_SERVER['DOCUMENT_ROOT'] match with my subdomains like so:

/
 var/
     www/
         html/
              .htaccess
              subdomain1.domain.com/
              subdomain2.domain.com/
              subdomain3.domain.com/

Where /var/www/html stand as 'DOCUMENT_ROOT'. Then put following code in the .htaccess file:

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/%{HTTP_HOST}/
RewriteRule (.*) /%{HTTP_HOST}/$1 [L]

It works as redirect wildcard subdomains to subdirectories, without changing URL in address bar.

Beside of vhost, you may also put the subdirectories outside root and access it using alias as described here. Then put the same .htaccess code in that location.

Community
  • 1
  • 1
eQ19
  • 9,880
  • 3
  • 65
  • 77