Redirect IIS 7.5-将url/index.php/*重定向到/*
我用cakePHP在IIS7下建立了一个php网站。它运行了很长一段时间,使用了不太漂亮的URL,格式为Redirect IIS 7.5-将url/index.php/*重定向到/*,redirect,rewrite,iis-7.5,Redirect,Rewrite,Iis 7.5,我用cakePHP在IIS7下建立了一个php网站。它运行了很长一段时间,使用了不太漂亮的URL,格式为/index.php/[controller]/[action]/[etc]。足够长的时间让人们有书签和谷歌索引一切。我现在已经修复了web.config,因此使用的url是来自/[controller]/[action]/[etc],绕过url中的index.php(在内部,所有url都位于同一位置) 问题是,如果你访问一个旧格式的链接,页面在技术上仍然可以正常工作,但是css/images
/index.php/[controller]/[action]/[etc]
。足够长的时间让人们有书签和谷歌索引一切。我现在已经修复了web.config
,因此使用的url是来自/[controller]/[action]/[etc]
,绕过url中的index.php
(在内部,所有url都位于同一位置)
问题是,如果你访问一个旧格式的链接,页面在技术上仍然可以正常工作,但是css/images/etc没有被加载,看起来很难看。有没有一种简单的方法可以将/index.php/*
格式重定向到新的/*
格式
以下是我现有的执行所需重写的web.config
:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect static resources" stopProcessing="true">
<match url="^(ico|img|css|files|js)(.*)$" />
<action type="Rewrite" url="app/webroot/{R:1}{R:2}" appendQueryString="false" />
</rule>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Rewrite" url="index.php?url={R:1}" appendQueryString="true" />
</rule>
<rule name="Imported Rule 2" stopProcessing="true">
<match url="^$" ignoreCase="false" />
<action type="Rewrite" url="/" />
</rule>
<rule name="Imported Rule 3" stopProcessing="true">
<match url="(.*)" ignoreCase="false" />
<action type="Rewrite" url="/{R:1}" />
</rule>
<rule name="Imported Rule 4" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Rewrite" url="index.php?url={R:1}" appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
我找到了我所缺少的东西。在重定向规则中,我匹配了
/index.php/*
,但我没有意识到您可以使用$0
、$1
、$2
等将匹配的字符串添加到目标中。另外,需要注意的是,exactDestination=“true”
,否则它会将/index.php/[controller]/[action]/[etc]
重定向到/[controller]/[action]/[etc]/index.php/[controller]/[action]/[etc]
我只需将以下部分添加到web.config
的顶部(在
部分,在
部分之前:
<httpRedirect enabled="true" exactDestination="true" httpResponseStatus="Permanent">
<add wildcard="/index.php/*" destination="/$0" />
</httpRedirect>