其实我们做PHP开发的,最常用的PHP环境无非是wamp,但目前为止,许多windows的服务器环境还都是IIS搭配的,所以从apache的htaccess做的伪静态就不得不重写了,因为IIS不支持htaccess,下面我们就来讲解一下如何将.htaccess文件转换成对应的web.config文件。
.htaccess
<IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQuEST_METHOD} ^TRACE RewriteRule .* - [F] #RewriteCond %{http_host} ^www.liqingbo.cn [NC] #RewriteRule ^(.*)$ http://liqingbo.cn/$1 [L,R=301] #RewriteLog "rewrite.log" #public目录,sys目录...,不进行重写 RewriteCond %{REQuEST_uRI} !^(/public(.*)|/Public(.*)|/sys(.*)|/Themes(.*))$ #这些后缀的文件,不进行RewriteRule RewriteCond %{REQuEST_uRI} !^.*(\.css|\.js|\.gif|\.png|\.jpg|\.jpeg|\.bmp)$ RewriteCond %{REQuEST_FILENAME} !-d RewriteCond %{REQuEST_FILENAME} !-f #RewriteBase /index.php RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L] RewriteRule /news/show-([0-9]+) /News/show/id/$1 RewriteRule /news/list-([0-9]+) /News/index/cid/$1 RewriteRule /product/show-([0-9]+) /product/show/id/$1 RewriteRule /product/list-([0-9]+) /product/index/cid/$1 RewriteRule /singlepage/show-([0-9]+) /Singlepage/show/id/$1 ErrorDocument 404 /404.html order deny,allow </IfModule>
转换后:
web.config
<?xml version="1.0" encoding="uTF-8"?> <configuration> <system.webServer> <rewrite> <rules> <!--#RewriteBase /index.php--> <rule name="product-show"> <match url="^product/show-([0-9]+).html" ignoreCase="false" /> <action type="Rewrite" url="/product/show/id/{R:1}" /> </rule> <rule name="已导入的规则 1" stopProcessing="true"> <match url="^(.*)$" ignoreCase="false" /> <conditions> <add input="{uRL}" pattern="^(/public(.*)|/Public(.*)|/sys(.*)|/Themes(.*))$" ignoreCase="false" negate="true" /> <add input="{uRL}" pattern="^.*(\.css|\.js|\.gif|\.png|\.jpg|\.jpeg|\.bmp)$" ignoreCase="false" negate="true" /> <add input="{REQuEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" /> <add input="{REQuEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" /> </conditions> <action type="Rewrite" url="index.php/{R:1}" appendQueryString="true" /> </rule> <rule name="已导入的规则 2"> <match url="/news/show-([0-9]+)" ignoreCase="false" /> <action type="Rewrite" url="/News/show/id/{R:1}" /> </rule> <rule name="已导入的规则 3"> <match url="/news/list-([0-9]+)" ignoreCase="false" /> <action type="Rewrite" url="/News/index/cid/{R:1}" /> </rule> <rule name="已导入的规则 5"> <match url="/product/list-([0-9]+)" ignoreCase="false" /> <action type="Rewrite" url="/product/index/cid/{R:1}" /> </rule> <rule name="已导入的规则 6"> <match url="/singlepage/show-([0-9]+)" ignoreCase="false" /> <action type="Rewrite" url="/Singlepage/show/id/{R:1}" /> </rule> </rules> </rewrite> </system.webServer> </configuration>
其实一开始伪静态是不起作用的,后来实在没办法了,只能死马当活马医了,随便捣腾了一下,还真可以实现了,不过到目前为止,我还是不太明白其中原因。
如图:
只要这两个为止互换一下,就不伪静态就不起作用了,真不知道为什么,还希望看明白的人能给我个提示,在下感激不尽。