linux空间下.htacess的几个用处

搜索引擎优化 04-26 阅读:167 评论:0

 

linux主机空间指定默认首页排序.

 

尤其是Godaddy的空间. 

默认首页文件是index.php排第一. 

这样会导致首页加载多出10s 

改成index.htm优先. 

新建.htaccess文件.内容为: 

<Files ~ "^.(htaccess|htpasswd)$"> 
deny from all 
</Files> 
DirectoryIndex index.html index.htm index.php 
order deny,allow

 

 

利用.htaccess文件做301重定向.

 

 

1.重定向heku.org到www.heku.org
这种重定向旨在使域名唯一,是网站SEO必须要做的,后面重定向www.heku.org到heku.org也是出于同样的原因,只是形式不同。
打开.htaccess文件,加入以下规则。(下面的规则是针对主域名的,子域名要修改)

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

2.重定向www.heku.org到heku.org

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

3.重定向oldheku.org到www.heku.org

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

4.重定向yaowu.org to heku.org

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

5.重定向domain.com/file/file.php 到 otherdomain.com/otherfile/other.php

RewriteCond %{HTTP_HOST} ^www.domain.com$
RewriteRule ^file/file.php$ http://www.otherdomain.com/otherfile/other.php [R=301,L]

6.yaowu.org整站重定向到heku.org,唯独首页跳转到另外一个url

RewriteEngine On
RewriteCond %{HTTP_HOST} !^yaowu.org$ [NC]
RewriteRule ^$ http://t.qq.com/bingdao [R=302,L]
RewriteRule ^(.*)$ http://www.heku.org/$1 [L,R=301]

 

最后一个代码,是 设置了默认首页排序跟301转向的. (不带www的域名转向到带www的域名.)

 

<Files ~ "^.(htaccess|htpasswd)$">

deny from all</Files>DirectoryIndex index.html index.htm index.php     order deny,allow  RewriteEngine OnRewriteCond %{HTTP_HOST} !^www.heku.org$ [NC]RewriteRule ^(.*)$ http://www.heku.org/$1 [L,R=301]

 

需要注意的是: php的301跳转可能导致 子目录绑定的域名也被301跳转.  强烈建议301的.htaccess写上后测试下子目录绑定的域名是否被跳转. 

godaddy后台中可以用 site redirect做不带www域名的301跳转.