1.重定向domain.com到www.domain.com
这种重定向旨在使域名唯一,是网站SEO必须要做的,后面重定向www.domain.com到domain.com也是出于同样的原因,只是形式不同。
打开.htaccess文件,加入以下规则。(下面的规则是针对主域名的,子域名要修改)
#301
[sourcecode language=”plain”]
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{http_host} ^domain.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301][/sourcecode]
www通用方法,上面的方法每个站都要去修改下,对于比较懒的人可以下面的这个方法
[sourcecode language=”plain”]
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
[/sourcecode]
2.重定向olddomain.com到www.newdomain.com
#301
[sourcecode language=”plain”]Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^aaaaa.com [NC]
RewriteRule ^(.*)$ http://www.aaaaa.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www.aaaaa.com [NC]
RewriteRule ^(.*)$ http://www.bbbbb.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^bbbbb.com [NC]
RewriteRule ^(.*)$ http://www.bbbbb.com/$1 [L,R=301][/sourcecode]
olddomain.com 为老域名
newdomain.com 为新域名
3.重定向www.domain.com到domain.com
[sourcecode language=”plain”]
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.domain\.com [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]
[/sourcecode]
或者使用通用方法
[sourcecode language=”plain”]
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.
RewriteCond %{HTTPS}s ^on(s)|off
RewriteCond http%1://%{HTTP_HOST} ^(https?://)(www\.)?(.+)$
RewriteRule ^ %1%3%{REQUEST_URI} [R=301,L]
[/sourcecode]
4.重定向到一个页面
[sourcecode language=”plain”]
Redirect 301 /oldpage.html http://www.example.com/newpage.html
Redirect 301 /oldpage2.html http://www.example.com/folder/
[/sourcecode]
5.目录别名
[sourcecode language=”plain”]
RewriteEngine On
RewriteRule ^source-directory/(.*) target-directory/$1
[/sourcecode]
6.脚本别名
[sourcecode language=”plain”]FallbackResource /index.fcgi[/sourcecode]
7.重定向整个网站
[sourcecode language=”plain”]Redirect 301 / http://newsite.com/[/sourcecode]
8.通过Referrer过滤访客
[sourcecode language=”plain”]
RewriteEngine on
# Options +FollowSymlinks
RewriteCond %{HTTP_REFERER} somedomain\.com [NC,OR]
RewriteCond %{HTTP_REFERER} anotherdomain\.com
RewriteRule .* – [F]
[/sourcecode]
9.防止被别的网页嵌套
[sourcecode language=”plain”]
SetEnvIf Request_URI "/starry-night" allow_framing=true
Header set X-Frame-Options SAMEORIGIN env=!allow_framing
[/sourcecode]
10.运行跨域字体引用
[sourcecode language=”plain”]
<IfModule mod_headers.c>
<FilesMatch "\.(eot|otf|ttc|ttf|woff|woff2)$">
Header set Access-Control-Allow-Origin "*"
</FilesMatch>
</IfModule>
[/sourcecode]
11.多域名访问
如果你不凑巧买到了不支持多域名的主机,那么.htaccess或许可以帮助你。现在假设你有域名domain-one.com和domain-two.com,并且在服务器根目录有对应文件夹one和two,那么通过下面的改写就能让Apache同时接受者两个域名的请求:
[sourcecode language=”plain”]
#two domains served from one root..
RewriteCond %{HTTP_HOST} domain-one.com
RewriteCond %{REQUEST_URI} !^/one
RewriteRule ^(.*)$ /one/$1 [L]
RewriteCond %{HTTP_HOST} domain-two.com
RewriteCond %{REQUEST_URI} !^/two
RewriteRule ^(.*)$ /two/$1 [L]
[/sourcecode]
12. 通过google搜索重定向跳转
[sourcecode language=”plain”]
RewriteEngine on
RewriteCond %{HTTP_REFERER} google [NC]
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [L]
[/sourcecode]
发表评论