我有一个带子域的Yii2 Advanced安装。 我的前端域名为http://xxx.tld,但www.xxx.tld也有效。
我希望将所有www请求重定向到非www。我尝试使用传统方法,但它似乎没有用。
我的所有子域名都有A记录,@
和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 contact@xxx.xxx
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
答案 0 :(得分: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>