简体   繁体   中英

URL Rewrite Rule Syntax Question

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?

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM