-1

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.

Unihedron
  • 10,902
  • 13
  • 62
  • 72
Fabio B.
  • 9,138
  • 25
  • 105
  • 177
  • 1
    Your lookbehind (if supported), is not at the good place, put it at the end of the pattern and add the dot (escaped) to avoid confusion with for example `phtml` – Casimir et Hippolyte Dec 11 '14 at 16:34
  • (.*[^/](?<!html))$ this works, but not in actual IIS7! Is that lookbehind supported? – Fabio B. Dec 11 '14 at 16:38
  • In this case, use a lookahead instead, but like that: `(?!.*\.html$).*[^/]$` – Casimir et Hippolyte Dec 11 '14 at 16:40
  • I'm trying that, indeed it works but it is always matching: (when I have .html the capturing group is always html) – Fabio B. Dec 11 '14 at 16:43
  • `(?!.*\.html$)(.*[^/])$` I haven't written the capture group. – Casimir et Hippolyte Dec 11 '14 at 16:47
  • Ok sorry, I didn't explain myself. Your regex works perfectly. http://mywebsite.my/it/products/brand/name/code.htm matches all and that's ok, http://mywebsite.my/it/products/brand/name/code.html matches "html", in that last case I don't want my pattern to match at all – Fabio B. Dec 11 '14 at 16:51

1 Answers1

0

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

I ended up with this:

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

Enough for me.

Fabio B.
  • 9,138
  • 25
  • 105
  • 177