通过 Apache 配置 Rewrite 支持 .htaccess
首先确定您使用的 Apache 版本,及是否加载了 mod_rewrite 模块。及是否加载了 mod_rewrite 模块 . mod_rewrite 的是一个 Apache 模块,可使服务器操纵请求的网址。根据一系列规则对传入的网址进行检查,规则中包含一个正则表达式来检测特定的格式。 如果在地址中发现了一个格式,并且满足适当的条件,该格式就会被一个替代的字符串或者是动作取代。
Apache 1.x 的用户请检查 conf/httpd.conf 中是否存在如下两段代码:
清单 1. Apache 1.x 的加载模块代码LoadModule rewrite_module libexec/mod_rewrite.so AddModule mod_rewrite.c |
Apache 2.x 的用户请检查 conf/httpd.conf 中是否存在如下一段代码:
清单 2. Apache 2.x 的加载模块代码LoadModule rewrite_module modules/mod_rewrite.so |
如果存在,那么在配置文件(通常就是 conf/httpd.conf)中加入如下代码。此时请务必注意,如果网站使用通过虚拟主机来定义,请务必加到虚拟主机配置,即 <VirtualHost> 中去,如果加在虚拟主机配置外部将可能无法使用,改好后将 Apache 重启。稍后再解释这段代码的意思
Apache配置:支持httpd.conf 配置和目录 .htaccess配置
启用rewrite
# LoadModule rewrite_module modules/mod_rewrite.so
去除前面的 #
LoadModule rewrite_module modules/mod_rewrite.so
启用.htaccess
AllowOverride None 修改为: AllowOverride All------------------------------------------------------------------------------让apache服务器支持.htaccess
如何让自己的本地APACHE服务器支持”.htaccess”呢?其实只要简单修改一下apache的httpd.conf设置就可以让APACHE支 持.htaccess了。打开httpd.conf文件用文本编辑器打开后,查找<Directory />Options FollowSymLinksAllowOverride None</Directory>改为<Directory />Options FollowSymLinksAllowOverride All</Directory>就可以了。RewriteEngine OnRewriteBase /伪静态RewriteRule ^(article)-([0-9]+)\.htm$ $1.php\?id=$2根据IP阻止用户访问order allow,denydeny from 127.0.0.1allow from allRedirects 转向Redirect /article.php http://www.baidu.com防止.htaccess文件被查看order allow,denydeny from all 制作错误提醒页面ErrorDocument 404 http://www.baidu.com添加 MIME 类型AddType application/x-shockwave-flash swfTips: 设置类型为 application/octet-stream 将提示下载改变缺省的首页文件 DirectoryIndex aa.html index.html index.pl default.htm防止目录列表时显示IndexIgnore *RewriteEngine OnRewriteBase /RewriteCond %{REQUEST_FILENAME} -s [OR]RewriteCond %{REQUEST_FILENAME} -l [OR]RewriteCond %{REQUEST_FILENAME} -dRewriteRule ^.*$ - [NC,L]RewriteRule ^([0-9a-zA-Z.]+)$ demo.php?id=$1mod_rewrite 规则修正符
1) R 强制外部重定向
2) F 禁用URL,返回403HTTP状态码。
3) G 强制URL为GONE,返回410HTTP状态码。
4) P 强制使用代理转发。
5) L 表明当前规则是最后一条规则,停止分析以后规则的重写。
6) N 重新从第一条规则开始运行重写过程。
7) C 与下一条规则关联
如果规则匹配则正常处理,以下修正符无效
8) T=MIME-type(force MIME type) 强制MIME类型
9) NS 只用于不是内部子请求
10) NC 不区分大小写
11) QSA 追加请求字符串
12) NE 不在输出转义特殊字符 \%3d$1 等价于 =$1
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.php100.com [NC]
RewriteRule ^(.*) http://www.php100.com/ [L]
RewriteEngine on
RewriteRule ^test([0-9]*).html$ test.php?id=$1 RewriteRule ^new([0-9]*)/$ new.php?id=$1 [R]
1、Rewrite规则简介:
Rewirte主要的功能就是实现URL的跳转,它的正则表达式是基于Perl语言。可基于服务器级的(httpd.conf)和目录级的 (.htaccess)两种方式。如果要想用到rewrite模块,必须先安装或加载rewrite模块。方法有两种一种是编译apache的时候就直接 安装rewrite模块,别一种是编译apache时以DSO模式安装apache,然后再利用源码和apxs来安装rewrite模块。
基于服务器级的(httpd.conf)有两种方法,一种是在httpd.conf的全局下直接利用RewriteEngine on来打开rewrite功能;另一种是在局部里利用RewriteEngine on来打开rewrite功能,下面将会举例说明,需要注意的是,必须在每个virtualhost里用RewriteEngine on来打开rewrite功能。否则virtualhost里没有RewriteEngine on它里面的规则也不会生效。
基于目录级的(.htaccess),要注意一点那就是必须打开此目录的FollowSymLinks属性且在.htaccess里要声明RewriteEngine on。
2、举例说明:
下面是在一个虚拟主机里定义的规则。功能是把client请求的主机前缀不是www.colorme.com和203.81.23.202都跳转到 主机前缀为http://www.colorme.com.cn,避免当用户在地址栏写入http://colorme.com.cn时不能以会员方式登 录网站。
NameVirtualHost 192.168.100.8:80
ServerAdmin webmaster@colorme.com.cn
DocumentRoot "/web/webapp" ServerName www.colorme.com.cn ServerName colorme.com.cn RewriteEngine on #打开rewirte功能 RewriteCond %{HTTP_HOST} !^www.colorme.com.cn [NC] #声明Client请求的主机中前缀不是www.colorme.com.cn,[NC]的意思是忽略大小写 RewriteCond %{HTTP_HOST} !^203.81.23.202 [NC] #声明Client请求的主机中前缀不是203.81.23.202,[NC]的意思是忽略大小写 RewriteCond %{HTTP_HOST} !^$ #声明Client请求的主机中前缀不为空,[NC]的意思是忽略大小写 RewriteRule ^/(.*) http://www.colorme.com.cn/ [L]#含义是如果Client请求的主机中的前缀符合上述条件,则直接进行跳转到http://www.colorme.com.cn/,[L]意味着立即停 止重写操作,并不再应用其他重写规则。这里的.*是指匹配所有URL中不包含换行字符,()括号的功能是把所有的字符做一个标记,以便于后面的应用.就是 引用前面里的(.*)字符。 例二.将输入 folio.test.com 的域名时跳转到profile.test.comlisten 8080
NameVirtualHost 10.122.89.106:8080 ServerAdmin webmaster@colorme.com.cnDocumentRoot "/usr/local/www/apache22/data1/"ServerName profile.test.comRewriteEngine on RewriteCond %{HTTP_HOST} ^folio.test.com [NC]RewriteRule ^/(.*) http://profile.test.com/ [L]3.Apache mod_rewrite规则重写的标志一览
1) R[=code](force redirect) 强制外部重定向
强制在替代字符串加上http://thishost[:thisport]/前缀重定向到外部的URL.如果code不指定,将用缺省的302 HTTP状态码。2) F(force URL to be forbidden)禁用URL,返回403HTTP状态码。3) G(force URL to be gone) 强制URL为GONE,返回410HTTP状态码。4) P(force proxy) 强制使用代理转发。5) L(last rule) 表明当前规则是最后一条规则,停止分析以后规则的重写。6) N(next round) 重新从第一条规则开始运行重写过程。7) C(chained with next rule) 与下一条规则关联如果规则匹配则正常处理,该标志无效,如果不匹配,那么下面所有关联的规则都跳过。
8) T=MIME-type(force MIME type) 强制MIME类型
9) NS (used only if no internal sub-request) 只用于不是内部子请求10) NC(no case) 不区分大小写11) QSA(query string append) 追加请求字符串12) NE(no URI escaping of output) 不在输出转义特殊字符例如:RewriteRule /foo/(.*) /bar?arg=P1\%3d$1 [R,NE] 将能正确的将/foo/zoo转换成/bar?arg=P1=zed13) PT(pass through to next handler) 传递给下一个处理例如:RewriteRule ^/abc(.*) /def$1 [PT] # 将会交给/def规则处理Alias /def /ghi 14) S=num(skip next rule(s)) 跳过num条规则15) E=VAR:VAL(set environment variable) 设置环境变量4.Apache rewrite例子集合
在 httpd 中将一个域名转发到另一个域名虚拟主机世界近期更换了域名,新域名为 www.wbhw.com, 更加简短好记。这时需要将原来的域名webhosting-world.com, 以及论坛所在地址 webhosting-world.com/forums/定向到新的域名,以便用户可以找到,并且使原来的论坛 URL 继续有效而不出现 404 未找到,比如原来的http://www.webhosting-world.com/forums/-f60.html, 让它在新的域名下继续有效,点击后转发到http://bbs.wbhw.com/-f60.html, 这就需要用 apache 的 Mod_rewrite 功能来实现。
在中添加下面的重定向规则:
RewriteEngine On
# Redirect webhosting-world.com/forums to bbs.wbhw.comRewriteCond %{REQUEST_URI} ^/forums/RewriteRule /forums/(.*) http://bbs.wbhw.com/$1 [R=permanent,L]# Redirect webhosting-world.com to wbhw.comRewriteCond %{REQUEST_URI} !^/forums/RewriteRule /(.*) http://www.wbhw.com/$1 [R=permanent,L]添加了上面的规则以后, 里的全部内容如下:
ServerAlias webhosting-world.com
ServerAdmin admin@webhosting-world.comDocumentRoot /path/to/webhosting-world/rootServerName www.webhosting-world.comRewriteEngine On# Redirect webhosting-world.com/forums to bbs.wbhw.comRewriteCond %{REQUEST_URI} ^/forums/RewriteRule /forums/(.*) http://bbs.wbhw.com/$1 [R=permanent,L]# Redirect webhosting-world.com to wbhw.comRewriteCond %{REQUEST_URI} !^/forums/RewriteRule /(.*) http://www.wbhw.com/$1 [R=permanent,L]URL重定向
例子一:
1.http://www.zzz.com/xxx.php-> http://www.zzz.com/xxx/
2.http://yyy.zzz.com-> http://www.zzz.com/user.php?username=yyy 的功能 RewriteEngine On RewriteCond %{HTTP_HOST} ^www.zzz.com RewriteCond %{REQUEST_URI} !^user\.php$ RewriteCond %{REQUEST_URI} \.php$ RewriteRule (.*)\.php$ http://www.zzz.com/$1/ [R] RewriteCond %{HTTP_HOST} !^www.zzz.com RewriteRule ^(.+) %{HTTP_HOST} [C] RewriteRule ^([^\.]+)\.zzz\.com http://www.zzz.com/user.php?username=$1例子二:
/type.php?typeid=* --> /type*.html
/type.php?typeid=*&page=* --> /type*page*.htmlRewriteRule ^/type([0-9]+).html$ /type.php?typeid=$1 [PT]RewriteRule ^/type([0-9]+)page([0-9]+).html$ /type.php?typeid=$1&page=$2 [PT]5.使用Apache的URL Rewrite配置多用户虚拟服务器
要实现这个功能,首先要在DNS服务器上打开域名的泛域名解析(自己做或者找域名服务商做)。比如,我就把 *.semcase.com和 *.semcase.cn全部解析到了我的这台Linux Server上。
然后,看一下我的Apache中关于*.semcase.com的虚拟主机的设定。
#*.com,*.osall.net
ServerAdmin webmaster@semcase.com
DocumentRoot /home/www/www.semcase.com ServerName dns.semcase.comServerAlias dns.semcase.com semcase.com semcase.net *.semcase.com *.semcase.net CustomLog /var/log/httpd/osa/access_log.log" commonErrorLog /var/log/httpd/osa/error_log.log"AllowOverride NoneOrder deny,allow #AddDefaultCharset GB2312 RewriteEngine on RewriteCond %{HTTP_HOST} ^[^.]+\.osall\.(com|net)$ RewriteRule ^(.+) %{HTTP_HOST}$1 [C] RewriteRule ^([^.]+)\.osall\.(com|net)(.*)$/home/www/www.semcase.com/sylvan$3?un=$1&%{QUERY_STRING} [L] 在这段设定中,我把*.semcase.net和*.semcase.com 的Document Root都设定到了 /home/www/www.semcase.com但是,继续看下去,看到...配置了吗?在这里我就配置了URL Rewrite规则。
RewriteEngine on #打开URL Rewrite功能RewriteCond %{HTTP_HOST} ^[^.]+.osall.(com|net)$ #匹配条件,如果用户输入的URL中主机名是类似 xxxx.semcase.com 或者 xxxx.semcase.cn 就执行下面一句RewriteRule ^(.+) %{HTTP_HOST}$1 [C] #把用户输入完整的地址(GET方式的参数除外)作为参数传给下一个规则,[C]是Chain串联下一个规则的意思RewriteRule ^([^.]+).osall.(com|net)(.*)$ /home/www/dev.semcase.com/sylvan$3?un=$1&%{QUERY_STRING} [L]# 最关键的是这一句,使用证则表达式解析用户输入的URL地址,把主机名中的用户名信息作为名为un的参数传给/home/www /dev.semcase.com目录下的脚本,并在后面跟上用户输入的GET方式的传入参数。并指明这是最后一条规则([L]规则)。注意,在这一句中 指明的重写后的地址用的是服务器上的绝对路径,这是内部跳转。如果使用http://xxxx这样的URL格式,则被称为外部跳转。使用外部跳转的话,浏 览着的浏览器中的URL地址会改变成新的地址,而使用内部跳转则浏览器中的地址不发生改变,看上去更像实际的二级域名虚拟服务器。Apache下禁止特定目录PHP,特定虚拟主机不支持PHP
有两种方式:方式一:.htaccess控制,适用于没有服务器管理权限。在可写文件夹的目录下,建一个.htaccess文件,内容为:<Files ~ ".php">Order allow,denyDeny from all</Files> 方式二:修改Apache配置文件,适用于有服务器管理权限。配置中增加如下内容:<Directory "特定目录地址"><Files ~ ".php">Order allow,denyDeny from all</Files></Directory>这样只特定目录不支持php,其他不受影响来自: