How to redirect www.domain.com to domain.com (or reverse)
Most sites can be reached by two different URLs. For example http://b2evolution.net/about/features.html and http://www.b2evolution.net/about/features.html .
This is good for the users who can choose whether or not to type in the "www." part. However, it is not good for search engines which will tend to see a lot of duplicate contents.
If you are on an Apache webserver, you can use mod_rewrite in order to redirect all traffic that goes to the www.domain to the "non www" domain. (If you want it the other way round, see below)
Try adding this to a file named ".htaccess" at the root of your site (create that file if it doesn't exist):
Code:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^your-domain\.com [NC]
RewriteRule ^/(.*) http://your-domain\.com/$1 [L,R=301]
Note: More advanced users will prefer to add this to the httpd.conf / apache2.conf instead in order to gain a little efficiency and also to not interfere with tests on development/staging servers.
Explanations
RewriteEngine on activates mod_rewrite.
The first RewriteCond makes sure we have a non(!) empty(^$) hostname to work with.
The second RewriteCond detects that the host name is not(!) the one we want (e-g: it has an extra www. part in it).
The RewriteRule sends out a permanent(301) redirection to the right domain name followed by the currently requested path/page ($1).
Handling multiple domains at once
Below is more elegant (yet complex to read) solution that can handle multiple domains at once (if you have "ServerAlias"es) and that doesn't require to type-in your domain (just copy/paste):
Code:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^/(.*) http://%1/$1 [L,R=301]
The trick here is to use %1 which matches the canonical part of the domain name in the second RewriteCond (.+) .
Reverse redirection
By popular demand, here is how to redirect from somedomain.com to www.somedomain.com
Code:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^/(.*) http://www.%{HTTP_HOST}/$1 [L,R=301]
【转自:http://fplanque.com/dev/http-ssl/www-domain-canonical-urls-mod_rewrite】
This is good for the users who can choose whether or not to type in the "www." part. However, it is not good for search engines which will tend to see a lot of duplicate contents.
If you are on an Apache webserver, you can use mod_rewrite in order to redirect all traffic that goes to the www.domain to the "non www" domain. (If you want it the other way round, see below)
Try adding this to a file named ".htaccess" at the root of your site (create that file if it doesn't exist):
Code:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^your-domain\.com [NC]
RewriteRule ^/(.*) http://your-domain\.com/$1 [L,R=301]
Note: More advanced users will prefer to add this to the httpd.conf / apache2.conf instead in order to gain a little efficiency and also to not interfere with tests on development/staging servers.
Explanations
RewriteEngine on activates mod_rewrite.
The first RewriteCond makes sure we have a non(!) empty(^$) hostname to work with.
The second RewriteCond detects that the host name is not(!) the one we want (e-g: it has an extra www. part in it).
The RewriteRule sends out a permanent(301) redirection to the right domain name followed by the currently requested path/page ($1).
Handling multiple domains at once
Below is more elegant (yet complex to read) solution that can handle multiple domains at once (if you have "ServerAlias"es) and that doesn't require to type-in your domain (just copy/paste):
Code:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^/(.*) http://%1/$1 [L,R=301]
The trick here is to use %1 which matches the canonical part of the domain name in the second RewriteCond (.+) .
Reverse redirection
By popular demand, here is how to redirect from somedomain.com to www.somedomain.com
Code:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^/(.*) http://www.%{HTTP_HOST}/$1 [L,R=301]
【转自:http://fplanque.com/dev/http-ssl/www-domain-canonical-urls-mod_rewrite】
本文介绍如何使用Apache的mod_rewrite模块来实现网站URL的规范化,避免搜索引擎因重复内容而降低网站权重。文章提供了两种重定向规则:一种是从www域名重定向到非www域名,另一种则是相反方向。
955

被折叠的 条评论
为什么被折叠?



