问题
I am trying to write a rule to permenantly redirect a domainname to another domain name
RewriteCond %{HTTP_HOST} ^www.companyname1.com$
RewriteRule ^(.*)$ http://www.companyname2.com/$1 [R=301,L]
This only works if the user remembers to type in www, if the user does not type in www in the url, the page will load but the image links will be broken.
Does anyone know how to adjust the above rule to it works with and without www?
I am using a LAMP configuration, apache 2 on redhat.
回答1:
You can supply several optional Rewrite-Conditions with [OR]:
RewriteCond %{HTTP_HOST} ^www.companyname1.com$ [OR]
RewriteCond %{HTTP_HOST} ^companyname1.com$
RewriteRule ^(.*)$ http://www.companyname2.com/$1 [R=301,L]
This should do the trick. The first Rewrite-Condition fires, if www is present, the second one fires, if www has been forgotten.
回答2:
The redirect was not working for me and I had to adjust it, below is a working version based on the answer by @Demento.
# Parmenent redirect to webdesign.danols.com of all pages
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.kingston-web-design.com [OR]
RewriteCond %{HTTP_HOST} ^kingston-web-design.com
RewriteRule ^(.*)$ http://webdesign.danols.com.com$1 [R=301,L]
回答3:
If you don't care what the hostname starts with then don't root the regex, just check that it ends with companyname1.com.
As for the leading slash just add it at as optional to the root of your regex.
RewriteCond %{HTTP_HOST} companyname1.com$
RewriteRule ^/?(.*) http://www.companyname2.com/$1 [R=permanent,L]
回答4:
Or just by using simple regex:
RewriteCond %{HTTP_HOST} ^(www.)?companyname1\.com$
RewriteRule ^(.*)$ http://www.companyname2.com/$1 [R=301,L]
来源:https://stackoverflow.com/questions/5553980/permanent-redirect-via-apache-rewrite-rules