开发者

htaccess rewritecond

We have a domain name with various TLDs.

Let's use example.com as our main URL, and we redirect example.biz, example.net, example.org to example.com.

We had the following in .htaccess file and it worked very well:

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

You notice that any non-www will be redirected to www.

However, we just added a subdomain: str.example.com, and in order to make it accessibl开发者_StackOverflow中文版e, we had to comment out the above rules.

I hope someone can help us to write the rules that will redirect:

  1. non-www and non-str to www
  2. non-.com TLDs to .com

Several cases to make my means clear:

  1. example.com -> www.example.com
  2. example.net -> www.example.com
  3. abc.example.com -> www.example.com
  4. str.example.com -> str.example.com
  5. str.example.org -> str.example.com

Thank you very much.


This is a bit more convoluted, but it saves you a potential extra redirect:

RewriteEngine On

RewriteCond %{HTTP_HOST} !^(www|str)\.        [NC,OR]
RewriteCond %{HTTP_HOST} !\.com$              [NC]
RewriteCond %{HTTP_HOST}  (.*?)\.[A-Z]+$      [NC]
RewriteCond %1            ^(([^.]+)\.)?example$
RewriteCond %2            ^(str)              [OR]
RewriteCond www           ^(www)
RewriteRule ^ http://%1.example.com%{REQUEST_URI} [R=301,L]

Note that it also only expects you to have a single TLD, so example.co.uk wouldn't work here for example. That wasn't one of your examples though, so I didn't attempt to account for it.


Try these rules:

RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\.[^/.]+$
RewriteCond %1 !^(www|str)$
RewriteRule ^ http://www.example.com%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\.([^/.]+)$
RewriteCond %2 !=com
RewriteRule ^ http://%1.example.com%{REQUEST_URI} [L,R=301]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜