Matching strings not ending with .html

青春壹個敷衍的年華 提交于 2019-12-14 03:24:20

问题


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.



来源:https://stackoverflow.com/questions/27427826/matching-strings-not-ending-with-html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!