IIS URL rewrite conflict between files and urls - iis

I have the below config for url rewrite in my IIS
<rewrite>
<rules>
<rule name="Rewrite CI Index">
<match url=".*" />
<conditions>
<add input="{REQUEST_FILENAME}" pattern="css|js|jpg|jpeg|png|gif|ico|htm|html|csv|ttf|woff|woff2|pdf|mp4|mov|ics" negate="true" />
<add input="{REQUEST_URI}" pattern="^/(php_scripts)" negate="true" />
<add input="{REQUEST_URI}" pattern="^/documents/intranet_documents/" negate="true" />
</conditions>
<action type="Rewrite" url="index.php/{R:0}" />
</rule>
</rules>
</rewrite>
The negate directive is there to prevent rewrite for website assets files (css, fonts, etc.) as well as other files from the documents upload/download section.
The issue I just came across is this, one of the urls is like this http://intranet/HR/Training-Basics however when I try to navigate to this url I get 404 because it is being caught by the negate function for file extension ics.
So, I need to be able to access this url, but at the same time I need to be able to download the "ics" files. What is the best way to do it?

You could modify the condition pattern from ics to\.ics, so that the rewrite rule will only rewrite based on file extension instead of specific string segment.
<rule name="Rewrite CI Index">
<match url=".*" />
<conditions>
<add input="{REQUEST_FILENAME}" pattern="\.(css|js|jpg|jpeg|png|gif|ico|htm|html|csv|ttf|woff|woff2|pdf|mp4|mov|ics)" negate="true" />
<add input="{REQUEST_URI}" pattern="^/(php_scripts)" negate="true" />
<add input="{REQUEST_URI}" pattern="^/documents/intranet_documents/" negate="true" />
</conditions>
<action type="Rewrite" url="index.php/{R:0}" />
</rule>

Related

How to fix IIS rewrite to work with / or %2F in url?

I use the following rules on my webserver to provide nicer urls:
<rule name="keresmind_redirect" stopProcessing="true">
<match url="^$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^mod=keres&nyid=999&keresoszo=([^=&]+)$" />
</conditions>
<action type="Redirect" url="mind/{C:1}/" appendQueryString="false" />
</rule>
<rule name="keresmind_rewrite" stopProcessing="true">
<match url="^mind/([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="?mod=keres&nyid=999&keresoszo={R:1}" />
</rule>
The problem is that when a user enters something with slash in "keresoszo", it gets converted to %2F like "...&keresoszo=a/c/d" becomes "/mind/a%2Fc%2Fd/" and iis gives me a 404 error because it thinks it's a different directory.
I read somewhere to replace %2F to %252F in the url but I couldn't figure out how.
One more thing: I have allowDoubleEscaping set to true.
Did anybody encounter the same error? Any help is really appreciated.

How to rewrite base path (i.e. "/") with IIS rewrite?

How do I rewrite site's base path (i.e. /) using IIS rewrite?
I've tried this so far:
<rule name="RewriteIndexUrl" stopProcessing="true">
<match url="/" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="index.html" />
</rule>
Currently it's not rewriting that base path, and it's returning the index.html directly.
I could figure out a possible solution and it worked:
Remove all default documents in IIS site (i.e. index.html, index.htm, default.aspx...)
Add the following rewriting rule:
<rule name="RewriteIndexUrl" stopProcessing="true">
<match url="^" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="false" />
</conditions>
<action type="Rewrite" url="index.html" />
</rule>

IIS URL Rewrite Rules

I have a AngularJS application that utilizes URL Rewriting for linking. My rewrite rule looks like :
<rewrite>
<rules>
<rule name="MainRule" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="Pattern" pattern="^api/(.*)" negate="false" />
</conditions>
<action type="Rewrite" url="Default.cshtml" />
</rule>
</rules>
I found this solution on SO: How do I configure IIS for URL Rewriting an AngularJS application in HTML5 mode? and made one modification to allow my API to pass through.
Essentially, I want to redirect all my requests to Default.cshtml EXCEPT calls to my Web API. This works GREAT when I'm I load the app on the base URL like: localhost/myapp. However, if I reload the page on a angular route like: localhost/myapp/dashboard it fails saying:
<Error><Message>No HTTP resource was found that matches the request URI 'http://localhost/myapp/dashboard'.</Message>
<MessageDetail>No type was found that matches the controller named 'dashboard'.</MessageDetail></Error>
I have a work around where I did:
<rewrite>
<rules>
<rule name="Default" stopProcessing="true">
<match url="^(?!lib|api|dist|assets|app/|bower|common|main|signalr|templates|bower_components/).*" />
<action type="Rewrite" url="Default.cshtml" />
</rule>
</rules>
</rewrite>
and it worked fine. Any ideas how I can utilize a cleaner solution above and still accomplish the rewrite?
I need to change the pattern and change {REQUEST_FILE} to {REQUEST_URI} in a couple of places. See my demo here:
<rewrite>
<rules>
<rule name="MainRule" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_URI}" matchType="Pattern" pattern="api/(.*)" negate="true" />
<add input="{REQUEST_URI}" matchType="Pattern" pattern="signalr/(.*)" negate="true" />
</conditions>
<action type="Rewrite" url="Default.cshtml" />
</rule>
</rules>
</rewrite>

IIS 7.5 URL Rewrite Module - Rewrite Maps with variables

I have several pages on my site that have long and unreadable URLs that I'd like to be able shorten. The problem I'm having is appending query strings with variable values.
For example:
"www.example.com/dir1/dir2/filename.php" shortens to "www.example.com/file".
"www.example.com/dir1/dir2/filename.php?id=2" would be "www.example.com/file/2".
"www.example.com/dir1/dir2/filename.php?id=2&alt=6" would be "www.example.com/file/2/6".
The values of 'id' and 'alt' are then used by our page to access information in our database, which determines the contents of the page. These values can change, and there is no set amount.
Right now I've got the first example working fine using the following rewrite rules:
<rewrite>
<outboundRules>
<remove name="OutboundRewriteUserFriendlyURL1" />
</outboundRules>
<rewriteMaps>
<rewriteMap name="StaticRewrites">
<add key="/file" value="/dir1/dir2/filename.php" />
</rewriteMap>
</rewriteMaps>
<rules>
<rule name="Rewrite Rule">
<match url=".*" />
<conditions>
<add input="{StaticRewrites:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Rewrite" url="{C:1}" />
</rule>
</rules>
</rewrite>
But I haven't been able to find anything that would allow the URLs to contain variables. Everything I've seen has used static rewrites like my current solution is using, and I can't find anything about allowing arbitrary parameters.
EDIT:
Found a better solution that doesn't use a Rewrite Map. I had attempted a simliar rule previously, but due to the IIS setup on our testing environment it wasn't working as expected. This version should work for most people.
<rule name="Curricula View" stopProcessing="true">
<match url="/file(?:/(\d+)(?:/(\d+))?)?" />
<action type="Rewrite" url="/dir1/dir2/filename.php?id={R:1}&alt={R:2}" appendQueryString="true" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
</rule>
Using this rule I was able to avoid using a rewrite map altogether. I had attempted to use something like this originally, but due to the setup of our test environment it wouldn't work without some weird tweaks. This version should work in all normal environments.
<rule name="Curricula View" stopProcessing="true">
<match url="/file(?:/(\d+)(?:/(\d+))?)?" />
<action type="Rewrite" url="/dir1/dir2/filename.php?id={R:1}&alt={R:2}" appendQueryString="true" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
</rule>
EDIT:
I was also able to get the original version with a rewrite map working for anyone interested.
<rule name="Rewrite Map with Variables" enabled="true">
<match url="^(.+?)/?/(.*)$" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="false">
<add input="{ProductMap:{R:1}}" pattern="(.+)" />
</conditions>
<action type="Rewrite" url="/dir1/dir2/filename.php?id={C:0}&other={R:2}" appendQueryString="true" />
</rule>

Use urlrewrite to rewrite all requests to root

I want to use web.config and IIS's urlrewrite to rewrite all requests to a certain directory to the root, for example:
From:
mydomain.com/directory/test.php
To:
mydomain.com/test.php
and
From:
mydomain.com/directory/test/test.php
To:
mydomain.com/test/test.php
All parameters, etc should be passed as well. Any idea how to do this using web.config?
I had a requirement for this a while back and found this:
Using Url rewrite to "delete" a folder
There's a small error in the rule which was corrected later by the poster. Here's a copy with the correction should the link dry up (I also made it a bit more generic for your requirements):
<rewrite>
<rules>
<rule name="RemoveDirectory" stopProcessing="true">
<match url="^directory$|^directory/(.*)$" />
<conditions>
</conditions>
<action type="Redirect" url="{R:1}" />
</rule>
<rule name="RewriteToFile">
<match url="^(?!directory/)(.*)" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="directory/{R:1}" />
</rule>
</rules>
</rewrite>
Just search and replace directory with your directory name.

Resources