RewriteRule ^(?!/index.php)(?!/themes)(?!/upload)(?!/static)(?!/application)(?!/st_plugins)(.*)$ /index\.php/$1 [I] php
ISAPI_Rewrite的URL伪静态规则 html
URL重写
一般的URL里面含有index.php,为了达到更好的SEO效果可能须要去掉URL里面的index.php ,经过URL重写的方式能够达到这种效果,一般须要服务器开启URL_REWRITE模块才能支持。
'URL_DISPATCH_ON'=>true,
'URL_MODEL'=>2, //开启了Rewrite,隐藏了index.php 服务器
首先是给iis 加上php 扩展(这就不赘述了),下载 ISAPI_Rewrite 并安装,而后给iis 加上该扩展,最关键是的 httpd.ini的配置信息了。也就是映射规则。 app
下面是个人Rewrite规则: 框架
[ISAPI_Rewrite] 网站
# 3600 = 1 hour
CacheClockRate 3600 google
RepeatLimit 32 spa
# Block external access to the httpd.ini and httpd.parse.errors files
RewriteRule /httpd(?:\.ini|\.parse\.errors).* / [F,I,O]
# Block external access to the Helper ISAPI Extension
RewriteRule .*\.isrwhlp / [F,I,O] xml
CodeIgniter框架 stblog 在iis下面去除index.php的方法: htm
使用的是iis7.5
[ISAPI_Rewrite]
# 3600 = 1 hour
CacheClockRate 3600
RepeatLimit 32
# Protect httpd.ini and httpd.parse.errors files
# from accessing through HTTP
RewriteEngine On
RewriteBase /
RewriteRule ^(?!/index.php)(?!/themes)(?!/uploads)(?!/application)(?!/st_plugins)(.*)$ /index\.php/$1 [I]
---------------------------如下为ISAPI_Rewrite仿盗链规则-----------------------------
#根据须要将容许访问的域名按下面例子添加便可。
#可根据须要自行设置须要防盗链的文件后缀。
#/block.html为盗链替换的网页,能够设置版权提醒。
# For version 2.x
# RewriteCond Host: ^(.+)$
# RewriteCond Referer: ^(?!http://\1.*).*$
# RewriteCond Referer: ^(?!http://(.*.baidu.com|.*.google.com|.*.g.cn).*).*$
# RewriteRule ^.*.(?:gif|jpg|jpeg|png|bmp|exe|rar|zip|mp3|wma)$ /block.html [I,O,N]
# For version 2.x
# RewriteCond Host: ^(.+)$
# RewriteCond Referer: ^(?!http://\\1.*).*$
# RewriteCond Referer: ^(?!http://(.*\.baidu\.com|.*\.google\.com|.*googlebot\.com).*).*$
# RewriteRule ^.*\.(?:gif|jpg|jpeg|png|bmp|exe|rar|zip)$ /block.gif [I,O,N]
# For version 3.x
# RewriteCond %{HTTP:Host} ^(.+)$
# RewriteCond %{HTTP:Referer} ^(?!http://\\1.*).*$
# RewriteCond %{HTTP:Referer} ^(?!http://(.*\.baidu\.com|.*\.google\.com|.*googlebot\.com).*).*$
# RewriteRule ^.*\.(?:gif|jpg|jpeg|png|bmp|exe|rar|zip)$ /block.gif [NC,N,O]
-------------------------------------------------------------------------------------
# IIS Rewrite
RewriteCond Host: (?:www|game)\.zozq\.com //ISAPI_Rewrite筛选对www.zozq.com域名起做用
RewriteRule (?!/index.php)(?!/admin.php)(?!/navi.xml)(?!/wwwroot)(?!/game)(?!/uc)(?!/Scripts)(?!/Uploads)(?!/Public)(?!/Common)(.*)$ /index\.php\?s=$1 [I]
//排除index.php;admin.php;Uploads;Public;Common等实际存在的文件及目录,或者添加如下规则也能够。
# RewriteRule ^(?!/index.php)(?!/admin.php)(?!/navi.xml)(?!/wwwroot)(?!/game)(?!/uc)(?!/Scripts)(?!/Uploads)(?!/Public)(?!/Common)(.*)$ /index.php/$1 [L]
····································································································································································
在Apache上开发的,.htaccess内容与TP手册中的同样,但IIS使用httpd.ini,而且规则不同。
IIS对于Rewrite规则
RewriteRule (.*)$ /index\.php\?s=$1 [I]
的解释是,无论什么样的请求URL,都重写到index.php入口文件
而Apache与IIS不同的地方是,对于规则
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?s=/$1 [QSA,PT,L]
</IfModule>
它对于请示的路径会作一些判断,当请求URL是一个存在的文件路径时,则不重写向index.php(本人对于URL REWRITE 中的REGEXP也不是很熟悉,难道APACHE的这行规则中有什么悬机?)
因此,使用了URL 重写,而且规则只有一条"将全部请求转向index.php",在APACHE下, Public目录仍然能够访问(里面但是CSS,JS和图片啊!),而到了IIS下则图片,CSS,JS都加载不进行,而且输入 /Public/ ,出现的错误是 没有Public这个模块!
解决方法是,IIS的重写规则不能简单的只指向index.php,须要使用正则表达的前瞻功能,排除指定路径,下面是个人网站的httpd.ini文件,
其中,Ask目录为一个问吧系统的路径,BBS是论坛的路径
[ISAPI_Rewrite]
RewriteRule (?!/ask)(?!/bbs)(?!/Public)(?!/Common)(.*)$ /index\.php\?s=$1 [I]
若是还要排除其它目录的话,只要将(?!/目录名)加在前面就好了
另外,IIS对于重写后的REQUEST_URI的解析也与Apache不同,若是使用了重写,IIS不会将在地址栏中的URL记为REQUEST_URI,而是重写后的URI 以下面的URL /pro/show/id/1.htm 使用重写 RewriteRule (.*)$ /index\.php\?s=$1 [I] 原本是执行Pro模块的show方法,为1,在Pro模块中获取REQUEST_URI,值应为/pro/show/id/1.htm 但在IIS中是 /index.php?s=/pro/show/id/1.htm 解决这个问题的方法以前也有了,在国外一个网站上看到的 //ISAPI_Rewrite 3.x if (isset($_SERVER['HTTP_X_REWRITE_URL'])){ $_SERVER['REQUEST_URI'] = $_SERVER['HTTP_X_REWRITE_URL']; } //ISAPI_Rewrite 2.x w/ HTTPD.INI configuration else if (isset($_SERVER['HTTP_REQUEST_URI'])){ $_SERVER['REQUEST_URI'] = $_SERVER['HTTP_REQUEST_URI']; //Good to go! } //ISAPI_Rewrite isn't installed or not configured else{ //Someone didn't follow the instructions! if(isset($_SERVER['SCRIPT_NAME'])) $_SERVER['HTTP_REQUEST_URI'] = $_SERVER['SCRIPT_NAME']; else $_SERVER['HTTP_REQUEST_URI'] = $_SERVER['PHP_SELF']; if($_SERVER['QUERY_STRING']){ $_SERVER['HTTP_REQUEST_URI'] .= '?' . $_SERVER['QUERY_STRING']; } //WARNING: This is a workaround! //For guaranteed compatibility, HTTP_REQUEST_URI or HTTP_X_REWRITE_URL *MUST* be defined! //See product documentation for instructions! $_SERVER['REQUEST_URI'] = $_SERVER['HTTP_REQUEST_URI']; } 上面的代码引入的话,便可解决IIS的REQUEST_URI方法(若是对于THINKPHP,其实上面的URL还有更简单的方法) $_SERVER['REQUEST_URI']=$_GET['s']; //直接将QS中的s变量赋值给$_SERVER['REQUEST_URI']就好了 上面已经解决了很多问题,但最后一个问题就是,也是我一直没有解决好的问题 对于下面的URL /pro/branch/air.htm?pclass=gree 在APACHE下面,能够获取到GET变量 pclass的值,而且branch变量的值为air(开启了伪静态),而在IIS下,branch值就不正确了,而pcalss则获取不到了 解决方法只有使用 /pro/branch/branch/air/pclass/gree这类的URL 或者本身在程序中从新解析URL。