1

I have a domain, lets say mydomain.com.

I want every subdomain of this to load the contents of the directory with the same name... For example, if someone writes

http://sub.mydomain.com/index.php

I want to show him the contents of

http://sub.mydomain.com/sub/index.php

Still, I want it to show in the addressbar the http://sub.mydomain.com/index.php

The apache vhost is like

<VirtualHost *:80>
  ServerName *.mydomain.com
  DocumentRoot /var/www/vhosts/mydomain.com/subdomains/subs/www
  ... more stuff ...
</VirtualHost>

so in the filesystem, the files for the example above would be under the directory

/var/www/vhosts/mydomain.com/subdomains/subs/www/sub

I tried many of the proposed solutions here, but most of them were redirects to some other domain/subdomain or end up in a redirect loop :(

tia

papas-source
  • 1,221
  • 1
  • 14
  • 20

2 Answers2

2

You need to add some rewrite rules that will examine the sub domain and then rewrite to the relevant path:

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

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

  DocumentRoot /var/www/vhosts/mydomain.com/subdomains/subs/www

  ... more stuff ...

</VirtualHost>

What this will do is capture anything preceding ".mydomain.com" then rewrite it into the URL as %1, $1 will be the requested resource such as index.html

Be aware this might trip you up if www.mydomain.com is a valid domain for your site!

arco444
  • 22,002
  • 12
  • 63
  • 67
  • Thank you very much for your reply! It is correct, you saved me a lot of time, but I cannot accept it until you edit your post and change the RewriteCond %{HTTP_HOST} (.*).\mydomain\.com to RewriteCond %{HTTP_HOST} (.*)\.mydomain\.com (small typo in escaping the dot) Once again, thank you very much. – papas-source Jan 14 '14 at 15:07
  • 1
    Good spot! Corrected now. – arco444 Jan 14 '14 at 15:21
0

Try this code in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^sub\.[^.]+\.[^.]+$ [NC]
RewriteRule !^sub(|/$) /sub%{REQUEST_URI} [L,NC]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Thank you!The thing is that i want EVERY subdomain to do such an internal redirection, not only the "sub" subdomain (as pointed out in my question) – papas-source Jan 14 '14 at 15:06