A-A+
虚拟主机windows iis7.5的301重定向web.config文件规则
经常玩winserver的人应该知道,在iis+php的环境配置下,如果想要做301重定向的话,也只有使用web.config的配置文件来定义了,其实在web.config下面定义也是很简单的,不过跟 .htaccess 文件的定向相比的话,优点却不是很明显了,在 .htaccess 下面可以单页面定向301转向,而在web.config下面则不行,也不知道是我自己设置url重定向有问题,还是怎么的,总之没有配置成功。
最后的解决方法是先转向到页面的index.php文件中,然后再由php进行单页面的转向,php做301重定向就很简单了,这里不再阐述,好了,web.config整站的重定向代码如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <configuration>
- <system.webServer>
- <rewrite>
- <rules><rule name="HTTP to HTTPS redirect" stopProcessing="true"><match url="(.*)" /><conditions><add input="{HTTPS}" pattern="off" ignoreCase="true" /></conditions><action type="Redirect" redirectType="Found" url="https://www.xiariboke.net/{R:1}" /></rule>
- <rule name="wordpress" patternSyntax="Wildcard">
- <match url="*" />
- <conditions>
- <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
- <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
- </conditions>
- <action type="Rewrite" url="index.php" />
- </rule></rules>
- </rewrite>
- </system.webServer>
- </configuration>
这是整站的301重定向代码,也是80端口的http重定向到 https 的配置,另外刚才也说了,如果单页面的重定向在 web.config 中我是定向失败的,不知道是不是跟 https 有关,从网上找到一段可以单页面重定向的代码:
- <configuration>
- <system.webServer>
- <rewrite>
- <rules>
- <rule name="b1 to news" stopProcessing="true">
- <match url="^b1pump.htm$" ignoreCase="true" />
- <action type="Redirect" url="http://www.xiariboke.net/luoganbeng/" />
- </rule>
- </rules>
- </rewrite>
- </system.webServer>
- </configuration>
这个是我从http://www.xiariboke.net/ptpin/b1pump.htm重定向到http://www.xiariboke.net/luoganbeng/不过要注意web.config是要放到ptpin/下面的就是旧的文章的目录下,不过我使用这段代码却没有定向成功,也许跟 https 有关,如果你有需要的话,也可以自定义尝试一下。