Matching strings not ending with .html

2019-09-20 15:58发布

问题:

I want to redirect my website users when they hit a REST path without the trailing slash.

Example.

http://mywebsite.my/it/products/brand/name => http://mywebsite.my/it/products/brand/name/
http://mywebsite.my/it/products => http://mywebsite.my/it/products/
http://mywebsite.my => http://mywebsite.my/

http://mywebsite.my/it/products/brand/name/code.html => ???

Well, I don't want the last one to be rewritten, I don't want the trailing slash when the URL ends with .html.

I'm working with URL rewrite module of IIS7, and this is my "slash-rule".

<rule name="SLASHFINALE" stopProcessing="true">
    <match url="(.*[^/])$" />
    <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    </conditions>
    <action type="Redirect" redirectType="Permanent" url="{R:1}/" />
</rule>

In other words, if the input url matches that regex (everything not ending with a slash), I rewrite the same URL adding the trailing slash.

So my rule would be the same, but with that little addition: rewrite all URLs, except the ones (already) having the trailing slash or the ones ending with ".html".

I wrote this

(.*(?<!html)[^\/])$

but I can't understand why it's not working.

回答1:

IIS Javascript-flavored regex parser does not support conditional expressions.

I ended up with this:

<match url="(.*[^\.]...[^\/]$)" />

Enough for me.