IIS 7.5 - 将url /index.php/*重定向到/ *

时间:2013-12-10 01:27:52

标签: redirect rewrite iis-7.5

我有一个使用cakePHP在IIS 7下设置的php网站。它使用/index.php/[controller]/[action]/[etc]形式的不那么漂亮的URL运行了很长一段时间。足够长的时间让人们有书签和谷歌索引一切。我现在修复了web.config,以便使用的网址来自/[controller]/[action]/[etc],绕过网址中的index.php(内部全部转到同一位置)。< / p>

问题是,如果您访问旧格式的其中一个链接,该页面仍然在技术上有效,但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>

1 个答案:

答案 0 :(得分:1)

我找到了我所缺少的东西。在重定向规则中,我在/index.php/*上匹配,但我没有意识到您可以使用$0$1$2等将匹配的字符串添加到目标中。另外值得注意的是,exactDestination="true"是必要的,否则会将/index.php/[controller]/[action]/[etc]重定向到/[controller]/[action]/[etc]/index.php/[controller]/[action]/[etc]

我只需将以下部分添加到web.config的顶部(<system.webserver>部分,<rewrite>部分之前:

<httpRedirect enabled="true" exactDestination="true" httpResponseStatus="Permanent">
  <add wildcard="/index.php/*" destination="/$0" />
</httpRedirect>
相关问题