【问题标题】:URL Rewrite Child Virtual DirectoryURL 重写子虚拟目录
【发布时间】:2015-02-06 18:08:54
【问题描述】:

想象一下我有这个网站:

http://localhost/site/admin

其中“site”是 IIS 虚拟目录,“admin”是“site”虚拟目录中的 IIS 虚拟目录。可以说“站点”和“管理员”都是独立的网站,因为一个是主网站,另一个是管理门户。我遇到的问题是 URL 的管理门户部分。

假设我有一个 index.php,它采用要显示的页面名称,例如:

http://localhost/site/admin/index.php?p=cats

我使用 URL 重写来允许将上面的 URL 写成这样:

http://localhost/site/admin/cats

我刚刚使用 IIS 规则向导创建了这种规则:

模式:^([^/]+)/?$
行动:index.php?p={R:1}

这很好用。

但是我现在正试图访问这样的 URL:

http://localhost/site/admin/cats/123

这实际上是:

http://localhost/site/admin/index.php?p=cats&id=123

我试图使用这样的模式:

^([^/]+)/([0-9]+)/?$

似乎该 URL 工作正常,但副作用是我所有的相对路径现在都不正确。我想要的是:

http://localhost/site/admin/pages/header.php

但它最终是:

http://localhost/site/admin/cats/pages/header.php

我认为这是因为重写后的 URL 在“admin”部分之后有“cats”,但我不知道如何使其正常工作。

谢谢!

【问题讨论】:

    标签: iis url-rewriting


    【解决方案1】:

    你至少应该做的是确保你不重写现有页面/内容(例如 header.php)。此外,您应该对从 URL 获得的部分进行 URL 编码。如果没有进行彻底的测试,这应该可以工作:

    <rule name="Rewrite">
        <match url="^(([^/]+)/)+([\w]+)/?([0-9]+)?$"/>
            <conditions>
                <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
                <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
            </conditions>
        <action type="Rewrite" url="index.php?p={UrlEncode:{R:3}}&id={UrlEncode:{R:4}}"/>
    </rule>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-25
      • 2016-04-15
      • 1970-01-01
      • 2015-08-30
      • 1970-01-01
      • 2011-02-12
      • 1970-01-01
      相关资源
      最近更新 更多