Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
297 views
in Technique[技术] by (71.8m points)

regex - Matching strings not ending with .html

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

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

1 Reply

0 votes
by (71.8m points)

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

I ended up with this:

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

Enough for me.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...