开发者

URL Rewrite Rule Syntax Question

开发者 https://www.devze.com 2023-04-03 08:04 出处:网络
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'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" r开发者_JAVA技巧edirectType="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?


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号