注意: 仅使用<VirtualHost>
参数。
如何将目录(比如example.com/groups/
)重定向到子域(比如groups.example.com
),以及将该子域的www(www.groups.example.com
)重定向到非目的地-www子域的网址(groups.example.com
)?
准确地说(⇒代表'应该301重定向到'):
example.com/groups/
⇒groups.example.com
www.groups.example.com
⇒groups.example.com
我已经读过,使用<VirtualHost>
优于所有通常使用的重定向规则(即在磁盘i / o上使用重定向引擎is pretty heavy)。所以,如果可能的话,我想在.htaccess / httpd.conf中使用<VirtualHost>
来解决上述问题。谢谢!
答案 0 :(得分:1)
这样的事情应该有效
<VirtualHost *:80>
ServerName example.com
ServerAlias groups.example.com www.groups.example.com
RewriteEngine on
# example.com/groups/ -> groups.example.com
RewriteCond %{SERVER_NAME} ^example\.com$
RewriteRule ^/(\w+)/?$ http://$1.example.com [R=301,L]
# www.groups.example.com -> groups.example.com
RewriteCond %{SERVER_NAME} ^www\.(\w+)\.example\.com$
RewriteRule ^/(.*)$ http://%1.example.com/$1 [R=301,L]
</VirtualHost>
或专门针对群组
<VirtualHost *:80>
ServerName example.com
ServerAlias groups.example.com www.groups.example.com
RewriteEngine on
# example.com/groups/ -> groups.example.com
RewriteCond %{SERVER_NAME} ^example\.com$
RewriteRule ^/groups/?$ http://groups.example.com [R=301,L]
# www.groups.example.com -> groups.example.com
RewriteCond %{SERVER_NAME} ^www\.groups\.example\.com$
RewriteRule ^/(.*)$ http://groups.example.com/$1 [R=301,L]
</VirtualHost>