URL 重写规则语法问题

发布于 2024-12-03 07:34:41 字数 1102 浏览 0 评论 0原文

我刚刚升级到 VS2010 / IIS 7.5 / URL Rewrite 2.0。我想做的事情很简单,但我真的厌倦了尝试独自完成它。

我只是想要干净 URL,即 http://example.com/abc-def .aspx 变为 http://example.com/abc-def/,有效地删除.aspx 扩展名并添加尾部斜杠。

我已经通过使用做到了这一点:

<rule name="Trim aspx for directory URLs">
    <match url="(.+)\.aspx$" />
    <action type="Redirect" redirectType="Permanent" url="{R:1}/" />
</rule>

效果很好,并按预期重定向,但没有拉出页面,所以我认为我需要将其与重写规则结合起来,以便它将干净的 URL 解析为相应的 .aspx页。

我尝试通过使用以下方法来做到这一点:

<rule name="Add aspx extension back internally">
    <match url="^http://example\.com/(.+)/$" ignoreCase="true" />
    <conditions>
        <add input="{URL}" matchType="IsDirectory" negate="true" />
        <add input="{URL}" pattern=".+/externals/.+" negate="true" />
    </conditions>
    <action type="Rewrite" url="{R:1}.aspx" />
</rule>

重定向规则有效,但内部重写规则似乎不起作用,因为页面没有拉起。我做错了什么?

I've just upgraded to VS2010 / IIS 7.5 / URL Rewrite 2.0. What I want to do is pretty easy I'd imagine, but I'm really tired of trying to get this to work on my own.

I simply want clean URLs, whereby http://example.com/abc-def.aspx becomes http://example.com/abc-def/, effectively removing the .aspx extension and adding a trailing slash.

I've done that by using:

<rule name="Trim aspx for directory URLs">
    <match url="(.+)\.aspx$" />
    <action type="Redirect" redirectType="Permanent" url="{R:1}/" />
</rule>

That works just fine and redirects as intended, but doesn't pull up the page so I thought I needed to combine that with a Rewrite rule so that it will resolve the clean URL to the corresponding .aspx page.

I've tried to do that by using:

<rule name="Add aspx extension back internally">
    <match url="^http://example\.com/(.+)/$" ignoreCase="true" />
    <conditions>
        <add input="{URL}" matchType="IsDirectory" negate="true" />
        <add input="{URL}" pattern=".+/externals/.+" negate="true" />
    </conditions>
    <action type="Rewrite" url="{R:1}.aspx" />
</rule>

The Redirect Rule works, but it seems as though the internal Rewrite Rule doesn't work because the page doesn't pull up. What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ˇ宁静的妩媚 2024-12-10 07:34:41

不太确定是否有办法使用 URL Rewrite 2.0 来完成这两项操作:

  1. 删除 .aspx 扩展名并在传入的请求上添加尾部斜杠 /。
  2. 在内部添加 .aspx 扩展名,以便加载正确的页面而不是得到 404。

我决定做的是更改源代码中的所有位置,使其指向无 .aspx 扩展名的 URL,这样就永远不会有以 .aspx 结尾的 URL 传入的外部请求。

这让我只需要:

<rule name="Add aspx extension back internally" stopProcessing="true">
    <match url="(.+)/$" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
    </conditions>
    <action type="Rewrite" url="{R:1}.aspx" />
</rule>

<rule name="Add trailing slash" stopProcessing="false">
    <match url="(.*[^/])$" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{URL}" pattern="favicon\.ico" ignoreCase="true" negate="true" />
        <add input="{URL}" pattern="\.axd" ignoreCase="true" negate="true" />
    </conditions>
    <action type="Redirect" url="{R:1}/" />
</rule>

Not really sure if there is a way with URL Rewrite 2.0 to do both:

  1. Remove .aspx extension and add trailing slash / on requests coming in.
  2. Internally add the .aspx extension back so that the correct pages loads instead of getting a 404.

What I decided to do was change everywhere in source to point to the .aspx extension-less URLs so that there should never be an outside request coming in with a URL ending in .aspx.

That allowed me to only need:

<rule name="Add aspx extension back internally" stopProcessing="true">
    <match url="(.+)/$" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
    </conditions>
    <action type="Rewrite" url="{R:1}.aspx" />
</rule>

<rule name="Add trailing slash" stopProcessing="false">
    <match url="(.*[^/])$" />
    <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{URL}" pattern="favicon\.ico" ignoreCase="true" negate="true" />
        <add input="{URL}" pattern="\.axd" ignoreCase="true" negate="true" />
    </conditions>
    <action type="Redirect" url="{R:1}/" />
</rule>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文