Redirect all pages of one TLD to another

五迷三道 提交于 2020-02-14 10:46:09

问题


\I'm darn nearly pulling my hair out trying to figure this out tonight. I'm hoping someone can help me out.

I have 3 TLD's for a site, similar to the following:

  • www.domain.com
  • www.domain.org
  • www.domain.net

They are all located in the same directory.

I would like to set up 301 redirects so that all pages of the .org and .net point to their respective pages at the .com location.

For example, domain.net/topic/page as well as www.domain.net/topic/page should permanently redirect to www.domain.com/topic/page.

Currently, I am using the code below which only redirects the .net and .org home pages to the .com home page.

RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com$ [NC]
RewriteRule .?$ http://www.domain.com%{REQUEST_URI} [R=301,L]

Thank you for your time,

Casey


回答1:


This one is based on your example with minor modification:

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

NC, R=301 and L are pretty obvious. The NE is for no-escape and it prevents query string variables from being escaped twice. The ^(.*) does not need a / in most cases.

Note: 301 permanent redirect responses will be cached by the browser so clear your browser cache every now and then while testing. Otherwise you may not see the result of changes you make.




回答2:


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

There are many other way to write the Cond, this is how I did mine - a positive compare. Yours is a negative compare, but it has the drawback that if someone doesn't send a host header it will redirect forever, on the plus you don't need to list all the domains to redirect. Also you can add a rule to redirect .com without www if you want.

For the Rule the trick is the () in the expression - this captures that part of the url for use later with the $1




回答3:


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

This redirects every request to www.domain.com including querystring and request-uri if the host is not www.domain.com



来源:https://stackoverflow.com/questions/7910740/redirect-all-pages-of-one-tld-to-another

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