Answer the question
In order to leave comments, you need to log in
Multiple apache 2.4 virtual hosts and redirect from www to non-www?
Let's say there are two sites: as and bs
Contain only the simplest index.html file. Virtual host configuration files a.conf and b.conf:
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName a.s
DocumentRoot /var/www/a
</VirtualHost>
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName b.s
DocumentRoot /var/www/b
</VirtualHost>
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName z.s
DocumentRoot /var/www/html
</VirtualHost>
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
. RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.a.s$ [NC]
RewriteRule ^(.*)$ http://a.s/$1 [R=permanent,L].
Answer the question
In order to leave comments, you need to log in
In your host settings it says: "ServerName as", so when the user comes to as, the server knows that this is the as host, and when the user comes to www.as, the server does not find such a host, the domain name is different, so it shows default entry. Just add ServerAlias to the host configs: ServerAdmin
[email protected]
ServerName as
ServerAlias www.as
DocumentRoot /var/www/a
And it will be possible to put .htaccess with a rewrite specific for each domain in its dokrut. When a user enters www.as, he will land on .htaccess for as, and in it you can already redirect him simply to as with a 301 redirect
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question