Redirect from few url's (both www and non-www) to original one

北城以北 提交于 2020-01-06 05:02:52

问题


How to shorten this expression? i want instead of using 2 lines just check whatever domain is something different from mysite.com e.g. mysite.org or www.mysite.org with or without www and redirect.

Options +FollowSymlinks
RewriteEngine On

RewriteCond %{HTTP_HOST} ^www\.mysite\.com$ [OR]
RewriteCond %{HTTP_HOST} ^mysite\.org$ [OR]
RewriteCond %{HTTP_HOST} ^www\.mysite\.org$ [OR]
RewriteCond %{HTTP_HOST} ^mysite\.net$ [OR]
RewriteCond %{HTTP_HOST} ^www\.mysite\.net$

RewriteRule ^(.*)$ http://mysite.com/$1 [R=301,L]

Also is it faster to use [OR] or do like this

  RewriteCond %{HTTP_HOST} ^www\.mysite\.com$
    RewriteRule ^(.*)$ http://mysite.com/$1 [R=301,L]
    RewriteCond %{HTTP_HOST} ^mysite\.org$
    RewriteRule ^(.*)$ http://mysite.com/$1 [R=301,L]
    RewriteCond %{HTTP_HOST} ^www\.mysite\.org$
    RewriteRule ^(.*)$ http://mysite.com/$1 [R=301,L]
    RewriteCond %{HTTP_HOST} ^mysite\.net$
    RewriteRule ^(.*)$ http://mysite.com/$1 [R=301,L]

P.S. why this works as case insensetive even when i dont use [NC]?


回答1:


You may want to check Apache documentation on canonical hostnames, your rules can be expressed as a negation

RewriteEngine On
RewriteCond %{HTTP_HOST}    !^mysite\.com$ [NC]
RewriteCond %{HTTP_HOST}    !^$
RewriteRule ^(.*)$  http://mysite.com/$1 [L,R=301]

And for the rules matching without [NC] , it probably depends on what the browser sends to the server.



来源:https://stackoverflow.com/questions/10361702/redirect-from-few-urls-both-www-and-non-www-to-original-one

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