基于URL在web.config文件中重定向

时间:2013-09-23 16:07:45

标签: redirect

我已经将我网站的文件结构更改为目录级别的深度。

因此,我希望用户点击:

http://example.com/directory/theRest/of/the/path

重定向到:

http://example.com/theRest/of/the/path

看起来像一个简单的web.config,对吗?

如何解析路径以简单地删除第一个目录并保持其余部分不受影响?

我想这个web.config会存在于“/ directory”中,但也许在root中可能会更好,这会匹配URL中“/ directory”的模式。

以下是我到目前为止(这可能是错误的)。非常感谢任何帮助!

<rule name="Redirect to main site" stopProcessing="true">
    <match url="^(.*)$" ignoreCase="true" />
    <conditions logicalGrouping="MatchAll">
        <add input="{REQUEST_FILENAME}" matchType="IsFile" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" />
    </conditions>
    <action type="Redirect" url="http://example.com/ ????" />
</rule>

1 个答案:

答案 0 :(得分:2)

您问题的简短回答是以下代码段:

<rule name="Redirect to main site" stopProcessing="true">
    <match url="^(directory\/)(.*)$" />
    <action type="Redirect" url="/{R:2}" />
</rule>

我已将URL路径分为两部分(按括号分组)。这允许我们从URL中提取directory{R:2}引用theRest/of/the/path,而{R:1}引用网址路径的第一部分 - 换言之,引用directory/