整理 IIS7配置web.config
程序员文章站
2024-01-24 22:25:22
...
会持续添加一些内容。。。。
web.config
<?xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer> <httpErrors> <remove statusCode="404" subStatusCode="-1" /> <!--统一修改为404.asp,因为这样返回状态码才会是404--> <error statusCode="404" prefixLanguageFilePath="" path="/404.asp" responseMode="ExecuteURL" /> </httpErrors> </system.webServer> <system.webServer> <!-- http://www.xxx.com 替换为对应的网站,带www--> <httpRedirect enabled="false" destination="http://www.xxx.com" exactDestination="false" childOnly="false" httpResponseStatus="Permanent" /> <!--启用Gzip压缩--> <urlCompression doStaticCompression="true" doDynamicCompression="false" /> <defaultDocument> <files> <clear /> <add value="index.html" /> <add value="index.php" /> <add value="index.htm" /> <add value="index.asp" /> </files> </defaultDocument> </system.webServer> <system.webServer> <rewrite> <rules> <rule name="WWW Redirect" stopProcessing="true"> <match url=".*" /> <conditions> <!--xxx.com,替换为对应的网站,不带www--> <add input="{HTTP_HOST}" pattern="^xxx.com$" /> </conditions> <!-- http://www.xxx.com 替换为对应的网站,带www--> <action type="Redirect" url="http://www.xxx.com/{R:0}" redirectType="Permanent" /> </rule> <rule name="Default Index"> <match url="^index.html$" ignoreCase="false" /> <action type="Redirect" url="/" appendQueryString="false" redirectType="Permanent" /> </rule> <rule name="Mobile Jump"> <match url="^(.*)$" /> <conditions logicalGrouping="MatchAll"> <add input="{HTTP_USER_AGENT}" pattern="nokia|iphone|android|motorola|symbian|sony|ericsson|mot|samsung|htc|sgh|lg|sharp|sie-|philips|panasonic|alcatel|lenovo|ipod|blackberry|meizu|netfront|ucweb|windowsce|palm|operamini|operamobi|opera|openwave|nexusone|cldc|midp|wap|mobile" /> </conditions> <!-- 输入要跳转的手机站点 --> <action type="Redirect" url="http://m.xxx.com/{R:1}" redirectType="Found" /> </rule> </rules> </rewrite> </system.webServer> </configuration>
web.config 功能的说明:
- 找不到页面设置为 404.asp 页面
- 启用Gzip压缩,仅压缩静态页面,不压缩动态页面
- 当客户端请求服务器时返回的默认文档顺序
- 不带www的域名跳转到带www
- 首页默认去掉index.html
- 手机跳转到对应的手机路径
首页301跳转指定的页面,其他页面301跳转对应的路径
<rule name="Index Redirect" stopProcessing="true"> <match url="(^index.html$)|(^$)" /> <action type="Redirect" url="http://www.xxx.com/index/" redirectType="Permanent" /> </rule> <rule name="Path Redirect" stopProcessing="true"> <match url="^(?!index.html).+$" /> <action type="Redirect" url="http://www.xxx.com/{R:0}" redirectType="Permanent" /> </rule>
整站301跳转对应的路径
<rule name="301Redirect" stopProcessing="true"> <match url="^(.*)$" /> <action type="Redirect" url="http://m.xxx.com/{R:0}" redirectType="Permanent" /> </rule>
不包含某字符串的URL,301跳转对应路径,str 改为该字符串
<rule name="Path Redirect" stopProcessing="true"> <match url="^((?!str).)*$" /> <action type="Redirect" url="http://www.xxx.com/{R:0}" redirectType="Permanent" /> </rule>
<!-- 多个字符串的匹配方式 --> <match url="^((?!(str1|str2|str3)).)*$" />
这是用来测试 web.config 的match url 正则是否通过,匹配的是路径部分。
<script type="text/javascript"> function isUrlMatch(url){ //var reg = new RegExp('/+'); //var reg = new RegExp('^(?!index).+$'); //var reg = new RegExp('^(index)$'); var reg = new RegExp('^(?!index.html).+$'); var bool = url.match(new RegExp(reg)); if(bool){ document.write("通过"); }else{ document.write("不通过"); } } isUrlMatch("dd/index.html"); </script>
统一隐藏路径的方法,'demo'改为要隐藏的路径
<?xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer> <rewrite> <rules> <rule name="OrgPage" stopProcessing="true"> <match url="^(.*)$" /> <conditions logicalGrouping="MatchAll"> <add input="{HTTP_HOST}" pattern="^(.*)$" /> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> </conditions> <action type="Rewrite" url="demo/{R:1}" /> </rule> </rules> </rewrite> </system.webServer> </configuration>
配置伪静态,将织梦的动态标签页面php变成html页面显示
<rule name="weather1" stopProcessing="true"> <match url="tags/([^-]+).html$" ignoreCase="true" /> <conditions logicalGrouping="MatchAll"> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> </conditions> <action type="Rewrite" url="/tags.php?/{R:1}" appendQueryString="false" /> </rule> <rule name="weather2" stopProcessing="true"> <match url="tags/([^-]+)-([0-9]+).html$" ignoreCase="true" /> <conditions logicalGrouping="MatchAll"> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> </conditions> <action type="Rewrite" url="/tags.php?/{R:1}/{R:2}" appendQueryString="false" /> </rule>
指定域名来手机跳转
<rule name="wwwdemocom"> <match url=".*" /> <conditions logicalGrouping="MatchAll"> <add input="{HTTP_HOST}" pattern="^www.demo.com$" /> <add input="{HTTP_USER_AGENT}" pattern="nokia|iphone|android|motorola|symbian|sony|ericsson|mot|samsung|htc|sgh|lg|sharp|sie-|philips|panasonic|alcatel|lenovo|ipod|blackberry|meizu|netfront|ucweb|windowsce|palm|operamini|operamobi|opera|openwave|nexusone|cldc|midp|wap|mobile" /> </conditions> <action type="Redirect" url="http://m.demo.com/" redirectType="Found" /> </rule>