IIS Rewrite 去除url里的index.php
发布时间:2016-08-09
RewriteRule ^(?!/index.php)(?!/themes)(?!/upload)(?!/static)(?!/application)(?!/st_plugins)(.*)$ /index\.php/$1


iis 伪静态规则 不重写目录
ISAPI_Rewrite的URL伪静态规则 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的配置信息了。也就是映射规则。下面是我的Rewrite规则:# 3600 = 1 hour CacheClockRate 3600RepeatLimit 32# Block external access to the httpd.ini and httpd.parse.errors files RewriteRule /httpd(?:\.ini|\.parse\.errors).* / # Block external access to the Helper ISAPI Extension RewriteRule .*\.isrwhlp / CodeIgniter框架 stblog 在iis下面去除index.php的方法:使用的是iis7.5 # 3600 = 1 hour CacheClockRate 3600RepeatLimit 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 ---------------------------以下为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 # 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 # 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 ------------------------------------------------------------------------------------- # IIS RewriteRewriteCond 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 //排除index.php;admin.php;Uploads;Public;Common等实际存在的文件及目录,或者添加以下规则也可以。# RewriteRule ^(?!/index.php)(?!/admin.php)(?!/navi.xml)(?!/wwwroot)(?!/game)(?!/uc)(?!/Scripts)(?!/Uploads)(?!/Public)(?!/Common)(.*)$ /index.php/$1 ····································································································································································在Apache上开发的,.htaccess内容与TP手册中的一样,但IIS使用httpd.ini,并且规则不一样。IIS对于Rewrite规则RewriteRule (.*)$ /index\.php\?s=$1 的解释是,不管什么样的请求URL,都重写到index.php入口文件 而Apache与IIS不一样的地方是,对于规则 RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php?s=/$1 它对于请示的路径会做一些判断,当请求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是论坛的路径 RewriteRule (?!/ask)(?!/bbs)(?!/Public)(?!/Common)(.*)$ /index\.php\?s=$1 如果还要排除其它目录的话,只要将(?!/目录名)加在前面就行了另外,IIS对于重写后的REQUEST_URI的解析也与Apache不一样,如果使用了重写,IIS不会将在地址栏中的URL记为REQUEST_URI,而是重写后的URI 如下面的URL /pro/show/id/1.htm 使用重写 RewriteRule (.*)$ /index\.php\?s=$1 本来是执行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。 【吉发布网】http://www.jifabu.com 文章来源
更多文章 进入论坛 我要发帖