2015-09-01 3 views
2

У меня есть установка Yii2 Advanced с поддоменами. Мой внешний домен http://xxx.tld, однако www.xxx.tld также работает.Yii2 Advanced на Apache2: перенаправление www на не-www

Я хочу, чтобы все запросы www были перенаправлены на не-www. Я пытался использовать традиционные методы, но он, похоже, не работает.

У меня есть запись для всех моих поддоменов и для @ и www

Вот мой конф файл для xxx.tld:

/apache2/sites-available/xxx.tld.conf

<VirtualHost *:80> 
# The ServerName directive sets the request scheme, hostname and port that 
# the server uses to identify itself. This is used when creating 
# redirection URLs. In the context of virtual hosts, the ServerName 
# specifies what hostname must appear in the request's Host: header to 
# match this virtual host. For the default virtual host (this file) this 
# value is not decisive as it is used as a last resort host regardless. 
# However, you must set it for any further virtual host explicitly. 
#ServerName www.example.com 
ServerName xxx.xxx 
ServerAdmin [email protected] 
DocumentRoot /var/www/xxx.xxx/frontend/web 


    <Directory "/var/www/xxx.xxx/frontend/web/"> 
    # Redirect www to non-www 
    RewriteEngine on 
    RewriteBase/
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC] 
    RewriteRule ^(.*)$ http://%1/$1 [R=301,L,NE] 


     # If a directory or a file exists, use the request directly 
     RewriteCond %{REQUEST_FILENAME} !-f 
     RewriteCond %{REQUEST_FILENAME} !-d 
     # Otherwise forward the request to index.php 
     RewriteRule . index.php 
    AllowOverride All 
     # ...other settings... 
    </Directory> 

    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn, 
    # error, crit, alert, emerg. 
    # It is also possible to configure the loglevel for particular 
    # modules, e.g. 
    #LogLevel info ssl:warn 

    ErrorLog ${APACHE_LOG_DIR}/error.log 
    CustomLog ${APACHE_LOG_DIR}/access.log combined 

    # For most configuration files from conf-available/, which are 
    # enabled or disabled at a global level, it is possible to 
    # include a line for only one particular virtual host. For example the 
    # following line enables the CGI configuration for this host only 
    # after it has been globally disabled with "a2disconf". 
    #Include conf-available/serve-cgi-bin.conf 
</VirtualHost> 

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet 

ответ

3

Поскольку у вас есть прямой доступ к главная конфигурация Apache, в первую очередь не нужно возиться с mod_rewrite. Хорошего старого RedirectPermanent в соответствующих контейнерах должно быть достаточно:

# 
# Secondary domains 
# 
<VirtualHost *:80> 
    ServerName www.xxx.tld 
    # Other alternatives domains: 
    ServerAlias example.net www.example.net 
    ServerAlias example.org www.example.org 

    RedirectPermanent/http://xxx.tld/ 
</VirtualHost> 


# 
# Main site 
# 
<VirtualHost *:80> 
    ServerName xxx.tld 

    DocumentRoot /var/www/xxx.xxx/frontend/web 
    # ... 
</VirtualHost> 
+0

Спасибо! Будет награждать награду, когда она станет доступной – Dean

Смежные вопросы