问题
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