Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
290 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

I ended up with this:

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

Enough for me.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...