我想将所有 example.org 301 重定向到 www.example.org。以下示例就是这样做的,但它包含很多干扰,因此很难维护且容易出错:
<VirtualHost 192.0.2.123:80>
ServerName example.org
RedirectPermanent / http://www.example.org
</VirtualHost>
<VirtualHost 192.0.2.123:80>
ServerName www.example.org
DocumentRoot /usr/local/www/example
</VirtualHost>
您是否知道上述内容是否有一些较短的版本?
像这样伪配置:
<VirtualHost 192.0.2.123:80>
ServerName www.example.org
ServerAlias example.org
# Redirect-Every-ServerAlias-To-ServerName
DocumentRoot /usr/local/www/example
</VirtualHost>
那么这样我只需要提供每个应该重定向的子域名ServerAlias
吗?
答案1
使用 mod_rewrite,如下所示:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.example.org [nocase]
RewriteRule ^(.*)$ http://www.example.org$1 [last,redirect=301]
答案2
<VirtualHost *:80>
ServerName www.example.org
DocumentRoot /usr/local/www/example
</VirtualHost>
<VirtualHost *:80>
ServerName example.org
ServerAlias *.example.org another.org *.another.org
Redirect 301 / http://www.example.org/
</VirtualHost>
确保按正确的顺序排列,www 优先