现在的位置: 首页 > HostGator主机常见问题 > 正文
浅谈HostGator主机Apache mod_rewrite实例
2013年02月18日 HostGator主机常见问题 ⁄ 共 1164字 暂无评论

大家都知道HostGator主机的PHP空间开放了mod_rewrite权限,mod_rewrite是apache的一个功能模块,利用mod_rewrite可以实现网站的伪静态,比如用户访问你的域名,你可以利用mod_revwrite功能让用户访问另一个页面,而实现的方法却很简单,只需要添加一些内容到你的.htaccess文件中即可,这个文件一般位于服务器上的public_html根目录下,下面和大家说说mod_rewrite常见的一些实例。

1、设置默认home.html为首页
DirectoryIndex home.html

2、只允许特定IP访问你的网站
deny from all
allow from 64.95.219.140
allow from 210.23.45.67

3、不使用带www的域名
#把下面例子中的’example.com’换成你自己的域名
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.(([a-z0-9_]+.)?example.com)$ [NC]
RewriteRule .? http://%1%{REQUEST_URI} [R=301,L]

4、使用带www的域名
# 把下面例子中的’example.com’换成你自己的域名
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([a-z.]+)?example.com$ [NC]
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteRule .? http://www.%1example.com%{REQUEST_URI} [R=301,L]

5、使用https对网站进行加密访问

# 把下面的‘www.example.com’换成你的域名
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

6、禁止多个特定网址过来的流量
RewriteEngine on
Options +FollowSymlinks
RewriteCond %{HTTP_REFERER} badsite.com [NC,OR]
RewriteCond %{HTTP_REFERER} badforum.com [NC,OR]
RewriteCond %{HTTP_REFERER} badsearchengine.com[NC]
RewriteRule .* – [F]

备注:Mod_rewrite模块包括很多工作,其中SEO人员最常用的就是在网站编辑时声明规则,被Apache实时地将访问者请求的静态URL地址映射为动态查询字符串,并发送给不同的PHP脚本处理,更多HostGator主机设置可以参考美国主机侦探论坛





抱歉!评论已关闭.