Using mod_rewrite to 301 to SERVER_NAME

佐手、 提交于 2019-12-22 11:27:53

问题


I have many ServerAlias'es in my Apache vhost and I want all domains that aren't the ServerName to be 301'ed to the ServerName.

This is not working:

RewriteCond %{HTTP_HOST} !%{SERVER_NAME} [NC]
RewriteRule ^(.*)$ http://%{SERVER_NAME}/$1 [R=301,QSA,L]

This does work:

RewriteCond %{HTTP_HOST} !www.some-domain.com [NC]
RewriteRule ^(.*)$ http://%{SERVER_NAME}/$1 [R=301,QSA,L]

Can I do this without hard coding the domain name?


回答1:


Environment variables are only expanded on the left hand side of RewriteCond. But you can use backreferences like this:

RewriteCond %{HTTP_HOST}/%{SERVER_NAME} !^([^/]+)/\1$



回答2:


the domain name is part of the CondPattern of RewriteCond.

the CondPattern is rendered on Startup of the httpd, so it can not contain any dynamic server variables ..

so this will not work ..



来源:https://stackoverflow.com/questions/4071883/using-mod-rewrite-to-301-to-server-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!