HostGator主机Apache mod_rewrite实例介绍

HostGator主机的PHP空间开放了mod_rewrite权限,mod_rewrite是apache的一个功能模块,利用mod_rewrite可以实现网站的伪静态,比如用户访问你的域名,这样只需要添加一些内容到你的.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] Continue reading