문제

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

도움이 되었습니까?

해결책

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!

다른 팁

Try this code in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^sub\.[^.]+\.[^.]+$ [NC]
RewriteRule !^sub(|/$) /sub%{REQUEST_URI} [L,NC]
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top