Domanda

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

È stato utile?

Soluzione

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!

Altri suggerimenti

Try this code in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^sub\.[^.]+\.[^.]+$ [NC]
RewriteRule !^sub(|/$) /sub%{REQUEST_URI} [L,NC]
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top