B
B
brar2016-04-10 18:06:39
linux
brar, 2016-04-10 18:06:39

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>

Also, there is a default virtual host zs, with a configuration file 000-default.conf.
<VirtualHost *:80>
        ServerAdmin [email protected]
        ServerName z.s 
        DocumentRoot /var/www/html
</VirtualHost>

/etc/apache2/apache2.conf
<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
</Directory>

Sites open in the browser by their names. Everything is OK.
Now for each site you need to make a 301 redirect:
www.as => as
www.bs => bs
Added a .htaccess file with contents to the root of the default site (zs).
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
.
Everything is redirected, everything is fine, in general.
If I create my own .htaccess file for each site, with a more specific condition, for example, for the as site, I do this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.a.s$ [NC]
RewriteRule ^(.*)$ http://a.s/$1 [R=permanent,L].

and so for each site with the corresponding changes,
then the content of the default site is opened without a redirect.
Thus, in my case, htaccess only works if it is placed on the default site.
Tell me, what settings should I fix so that each site has its own htaccess?
After all, for example, when we have different cms and each has its own htaccess, how do we make our own redirect for each site?
Create an empty default host 000-default with your own htaccess? or what?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Pyanov, 2016-04-14
@brar

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 question

Ask a Question

731 491 924 answers to any question