A-A+

虚拟主机windows iis7.5的301重定向web.config文件规则

2017年09月29日 PHP技术文章 暂无评论 阅读 0 views 次

经常玩winserver的人应该知道,在iis+php的环境配置下,如果想要做301重定向的话,也只有使用web.config的配置文件来定义了,其实在web.config下面定义也是很简单的,不过跟 .htaccess 文件的定向相比的话,优点却不是很明显了,在 .htaccess 下面可以单页面定向301转向,而在web.config下面则不行,也不知道是我自己设置url重定向有问题,还是怎么的,总之没有配置成功。

最后的解决方法是先转向到页面的index.php文件中,然后再由php进行单页面的转向,php做301重定向就很简单了,这里不再阐述,好了,web.config整站的重定向代码如下:

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <configuration>  
  3.   <system.webServer>  
  4.     <rewrite>  
  5.   
  6.       <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>  
  7.             <rule name="wordpress" patternSyntax="Wildcard">  
  8.                 <match url="*" />  
  9.                     <conditions>  
  10.                         <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />  
  11.                         <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />  
  12.                     </conditions>  
  13.                 <action type="Rewrite" url="index.php" />  
  14.             </rule></rules>  
  15.     </rewrite>  
  16.   </system.webServer>  
  17. </configuration>  

这是整站的301重定向代码,也是80端口的http重定向到 https 的配置,另外刚才也说了,如果单页面的重定向在 web.config 中我是定向失败的,不知道是不是跟 https 有关,从网上找到一段可以单页面重定向的代码:

  1. <configuration>  
  2.   <system.webServer>  
  3.     <rewrite>  
  4.       <rules>  
  5. <rule name="b1 to news" stopProcessing="true">   
  6.     <match url="^b1pump.htm$" ignoreCase="true" />   
  7.     <action type="Redirect" url="http://www.xiariboke.net/luoganbeng/" />   
  8. </rule>  
  9.       </rules>  
  10.     </rewrite>  
  11.   </system.webServer>  
  12. </configuration>  

这个是我从http://www.xiariboke.net/ptpin/b1pump.htm重定向到http://www.xiariboke.net/luoganbeng/不过要注意web.config是要放到ptpin/下面的就是旧的文章的目录下,不过我使用这段代码却没有定向成功,也许跟 https 有关,如果你有需要的话,也可以自定义尝试一下。

标签:

给我留言