IIS URL Rewrite module issue when trying to add trailing slash - asp.net

My company launched a redesigned version of our site this past October. We have a normal ASP.NET domain setup, but some pages were renamed and moved to new locations, so we had to rely on the URL Rewrite module in Server 2012 to ensure that bookmarks were routing to the correct locations.
In most cases, a 1:1 URL match based on regex patterns or a rewrite map works fine and as you would expect. However, we have some legacy directories that I had to move over as well, that weren't a part of the redesign project. I moved these folders into a folder off of the root, something like:
$/sandbox/annualreport/
so that we can monitor and update all of the folders that haven't been redesgined in one place. The abridged rewrite entry I am using is this:
<rewrite>
<rules>
<clear />
<rule name="Sandbox URLs">
<match url="^(annualreport)\b/?(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Rewrite" url="sandbox/{R:1}/{R:2}" />
</rule>
</rules>
</rewrite>
This works fine for all URLs when the directories are followed by a trailing slash, like $/sandbox/annualreport/ but not when the trailing slash is left off, like $/sandbox/annualreport, even though the / is optional. The pages load and they are accessed correctly, but the problem is, some of these directories have local css, images, and scripts, which are accessed in the HTML like href="css/main.css" or src="scripts.js". When I view source and click on those style or js links, the rewrite looks for these files off of the root, unless the trailing slash is added.
This has become a nuisance because I have to have a 301 redirect for every "sandbox URL" (before this rewrite entry) to add the trailing slash. This works, but I have to specify every folder name in the regex:
<rule name="Custom Add Trailing Slash" stopProcessing="false">
<match url="^(annualreport)\b$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Redirect" url="{R:0}/" />
</rule>
I have tried the default URL Rewrite wizard for universally adding a trailing slash, which I would prefer, but it doesn't seem to fix the localized files problem:
<rule name="AddTrailingSlashRule1" stopProcessing="false">
<match url="(.*[^/])$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="{R:1}/" />
</rule>
How can I force a trailing slash to be added on a URL rewrite while maintaining the locally accessed files within the rewrite directories?
Any help would be greatly appreciated. Thanks.

Related

IIS rewrite and redirect rules doesn't work on my site

Hi have two problems using IIS rewrite and redirect rules in front of a web page hosted on GitHub Pages.
Rewrite to GitHub Page
Update: Everything worked perfectly. As described later in this question, it was GitHub Pages that did the redirect to the canonical URL. Adding a rewrite appending the missing trailing slash before hitting GitHub Pages fixed it:
<rule name="Add Trailing Slash" stopProcessing="true">
<match url=".*[^/]$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" pattern=".+?\.\w+$" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="{ToLower:{R:0}}/" />
</rule>
The site is hosted on GitHub Pages and available here: http://elmahio.github.io/blog/.
In order to rewrite requests to my proxy, I have added the following rewrite rule:
<rule name="Proxy" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_URI}" negate="true" pattern="^(.*)/.well-known/(.*)$"/>
</conditions>
<action type="Rewrite" url="http://elmahio.github.io/blog/{R:1}" />
</rule>
The rewrite rule works like a charm. But if anyone forget the trailing slash to an URL, it seems to redirect to the GitHub Page version. Example:
https://blog.elmah.io/great-dot-net-conferences-to-attend redirects to http:// elmahio.github.io/blog/great-dot-net-conferences-to-attend/
I can see that GitHub Pages automatically append the trailing slash if not there. This might even be the main problem, since my proxy rewrite to the GitHub one and then redirect to append the trailing slash.
Any ideas to how to fix this? Maybe add a redirect rule in my proxy, appending that slash before rewriting to GitHub Pages?
Redirect to HTTPS
Update: Removing my custom redirect rule and enabling HTTPS Only on the Custom domain view on Azure, fixed this problem
I want all requests made to a non-https URL, redirected to HTTPS. I have added a redirect rule to web.config like this:
<rule name="RedirectToHTTPS" stopProcessing="true">
<match url="(.*)"/>
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true"/>
</conditions>
<action type="Redirect" url="https://{SERVER_NAME}/{R:1}" redirectType="Permanent"/>
</rule>
The rewrite rule works fine on URL's containing a path. Example:
http://blog.elmah.io/great-dot-net-conferences-to-attend/ redirects to https://blog.elmah.io/great-dot-net-conferences-to-attend/
But when visiting the front page (http://blog.elmah.io/ or http://blog.elmah.io) without HTTPS, I am not redirected to the HTTPS URL.
What could go wrong here?
All your rewrites are correct. Most probably issue exists because of GitHub pages. Try to do the following:
1) Create a rule to append trailing slash. You can find the example of rewrite rule in this thread IIS URL Rewrite: Add trailing slash except for .html and .aspx
2) Instead of using rewrite rule for redirect to HTTPS, you can enable HTTPS Only on the Custom domain view on Azure

Remove Trailing Slash From the URL

I want to redirect "abc.aspx/" to "abc.aspx". How can this be done?
Page gets broken when requested page ends with '/'. How to handle such type of requests?
Is there any rewriting rule that can be added into web.config file?
In your web.config, under the system.webServer, add
<rewrite>
<rules>
<!--To always remove trailing slash from the URL-->
<rule name="Remove trailing slash" 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>
</rules>
</rewrite>
Some Gotchas
In your development environment, if you run your web site under Visual Studio Development Sever, you won't be able to see this feature working. You will need to configure your application to run under at least IIS Express.
When you deploy your web site and see that this feature is not working on your production server, it will be because you miss configured something. One of the common mistakes is having overrideModeDefault attribute set to Deny for rules under <sectionGroup name="rewrite"> inside your applicationHost.config file.
If you are on a shared hosting environment and you see that this feature is not working, ask your provider if they have given you the permission of configuring this part.
Source: http://www.tugberkugurlu.com/archive/remove-trailing-slash-from-the-urls-of-your-asp-net-web-site-with-iis-7-url-rewrite-module
I know this is an old post, but Mihai's answer can make your application vulnerable to open redirects attack (see this)
<action type="Redirect" redirectType="Permanent" url="{R:1}" />
This redirect URL can be used for an external redirect, so make sure you have other validations in place before this rule is evaluated or change the redirect to make it internal instead. If you really want to remove the trailing slash and keep it internal, you can change it to:
<action type="Redirect" redirectType="Permanent" url="/{R:1}" />
Cheers

IIS URL rewrite module url's to lowercase

For better SEO we are using URL rewrite to convert all the URL's to lowercase. I set this one as mentioned in this the below article.
Everything is working fine from URL perspective, but we see lot of 301 redirects when we check in fiddler.
It looks like the images, javascript, css, jquery ajax calls and everything is getting converted into lower case.
I am trying to remove that and want to rewrite only aspx extension and no extension urls. I tried to play around the matchurl without any success. Any help or guidelines will be highly appricated.
Thanks
Edit:
My Current rule is
<rules>
<rulename="LowerCaseRule1"patternSyntax="ExactMatch"stopProcessing="true">
<matchurl="[A-Z]"ignoreCase="false"/>
<actiontype="Redirect"url="{ToLower:{URL}}"/>
</rule>
</rules>
You could probably use something as follow:
<rule name="LowerCaseRule1" stopProcessing="true">
<match url="[A-Z]" ignoreCase="false" />
<action type="Redirect" url="{ToLower:{URL}}" />
<conditions logicalGrouping="MatchAny">
<add input="{REQUEST_FILENAME}" pattern="\.aspx$" />
<add input="{REQUEST_FILENAME}" pattern="\." negate="true" />
</conditions>
</rule>
The rule will be triggered only if one of the condition is true:
The first one checks if the requested path (filename) ends with .aspx.
The second one checks if the if the requested path (filename) doesn't contain a . (so doesn't have an extension)

Simple trailing slash URL rewrite

I've never done URL rewriting (redirecting). I have a website http://sub.sub.domain.ext/app/. "app" signifies an "Application", not a virtual directory.
When a user navigates to http://sub.sub.domain.ext/app (no slash) I need the IIS 7 to redirect him to the URL with the trailing slash.
The thing is that I want the rule to be applied only when the user navigates to application. I don't want the trailing slash to be appended to every filename.
I have tried modifying the predefined rule in IIS7 manager but with no success. I've tried exact matching the whole URL, constraining the conditions, or simply using the original predefined rule. But even when using the original rule, it rewrites all subsequent request files/dirs/URLs, but it does not redirect the user from http://sub.sub.domain.ext/app to http://sub.sub.domain.ext/app/.
The rule you are looking might be as simple as:
<rule name="Add trailing slash" stopProcessing="true">
<match url="^app$" negate="false" />
<action type="Redirect" url="{R:0}/" />
</rule>
The url="^app$" pattern matches only the url http://www.yourwebsite.com/app and nothing else.
If you need to limit this behavior to the host sub.sub.domain.ext, you can add a condition:
<rule name="test" stopProcessing="true">
<match url="^app$" negate="false" />
<action type="Redirect" url="{R:0}/" />
<conditions>
<add input="{HTTP_HOST}" pattern="^sub.sub.domain.ext$" />
</conditions>
</rule>

IIS Forces Slash even with URL Rewrite to remove it

I am unable to remove the trailing slash my site's URLs even with the URL rewrite from: http://ruslany.net/2009/04/10-url-rewriting-tips-and-tricks/.
Frustrating really since it should be so simple but my attempts have not produced any results.
I even went as far as to create a test directory and added file called desktops.aspx and and sub folder called desktops.
without the sub directory "/test/desktops" loads fine since i set the default document to look at desktops.aspx.
with a subfolder created and still referencing "/test/desktops" it forces the slash and looks at the sub directory.
Why does IIS does this since its supposed to look for the file first then the sub directory correct? Are there any settings on the server side that would force back the slash?
URL Rewrite Snippet:
<rule name="SEO - Remove trailing slash" stopProcessing="false">
<match url="(.+)/$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_METHOD}" pattern="GET" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Rewrite" url="_{R:1}" />
</rule>
any help would be welcome
You are using an action of type Rewrite but you want a Redirect.
Change your configuration to:
<rule name="SEO - Remove trailing slash" stopProcessing="false">
<match url="(.*)/$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="{R:1}" />
</rule>
You also need to change url="(.+)/$" to url="(.*)/$".
TIP:
The best way to test your pattern is to use the IIS test pattern tool.
At the root of your website -> URL Rewrite -> Create a blank rule -> click on test pattern:
I was having this same problem and here is what I found.
My intent was to use this rule on an MVC website but I didnt want to test in production so I tested the rule on a site already setup which happened to be web forms asp.net.
I encountered the same problem as you. Navigating to www.example.com/test redirected to www.example.com/test/ even with the rule in place.
So I noticed the conditions to check if the requested url is a file or directory and I removed them.
Now going to www.example.com/test/ redirected to www.example.com/test. Yay! No. IIS automatically added another redirect back to www.example.com/test/ resulting in a redirect loop. Boo.
I then found this article https://support.microsoft.com/en-us/kb/298408 which relates to IIS 6 but is obviously still an issue.
So because I am in an asp.net web forms site, in my case /test is a physical directory and there is something in IIS that forces the trailing slash for directories. And sorry to say but I couldn't find a way to easily turn it off.
However! My requirement was for MVC and configured routes that are NOT directories. So I tried it again on a MVC website and the redirect to remove the trailing slash worked perfectly.
The rule I ended up with is:
<rule name="RemoveDatSlash" 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>
Hope this helps!
The problem I had were links to PDF files having trailing forward slashes, so what worked for me in Windows Server 2008 R2 running IIS 6.1:
Click on the website that needs the rule and in the Features view open up URL Rewrite and then on the Actions section (right pane) choose Add Rule(s) and select Append or Remove the trailing slash symbol. In the next window on the drop down choose remove if exists.
Hope this helps.

Resources