IIS Rewrite 去除url裏的index.php

RewriteRule ^(?!/index.php)(?!/themes)(?!/upload)(?!/static)(?!/application)(?!/st_plugins)(.*)$ /index\.php/$1 [I] php


iis 僞靜態規則 不重寫目錄

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。

相關文章
相關標籤/搜索