Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IIS URL Rewrite: Add trailing slash except for .html and .aspx

Tags:

Adding a trailing slash to all URLs through IIS URL Rewrite Module is widely spread, but how do I add exceptions for URLs that ends with .html and .aspx?

Today I have this:

<rule name="Add trailing slash" stopProcessing="true">   <match url="(.*[^/])$" />   <conditions>     <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />     <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />     <!-- Doesn't seem to be working -->     <!--<add input="{REQUEST_URI}" pattern="(.*?).html$" negate="true" />-->     <!--<add input="{REQUEST_URI}" pattern="(.*?).aspx$" negate="true" />-->   </conditions>   <action type="Redirect" redirectType="Permanent" url="{R:1}/" /> </rule> 
like image 380
Seb Nilsson Avatar asked Oct 04 '12 09:10

Seb Nilsson


People also ask

How do you add a trailing slash to a URL?

A trailing slash is a forward slash (“/”) placed at the end of a URL such as domain.com/ or domain.com/page/. The trailing slash is generally used to distinguish a directory which has the trailing slash from a file that does not have the trailing slash.

How do you fix trailing slash issues?

A 301 redirect is the best way to resolve duplicate content issues caused by trailing slashes. If you're just fixing one page, you'd redirect the duplicate copy to the version that matches your chosen URL structure. Most trailing slash issues however, affect many pages across a website.

Should I add trailing slash to URL?

If your site has a directory structure, it's more conventional to use a trailing slash with your directory URLs (for example, example.com/directory/ rather than example.com/directory ), but you can choose whichever you like. Be consistent with the preferred version. Use it in your internal links.


1 Answers

If you want something done right, you've got to do it yourself, obviously...

Here is the solution to my question:

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

Update: I blogged about this in more detail.

like image 114
Seb Nilsson Avatar answered Sep 20 '22 18:09

Seb Nilsson