Rewriting URLs in IIS7 - iis

I have been following http://learn.iis.net/page.aspx/806/seo-rule-templates/, which is a nearly perfect guide to creating SEO friendly URLs in IIS7.
I have one problem though:
If I create a rule to rewrite www.domain.com/contact/ I get in web.config:
<rule name="RewriteUserFriendlyURL1" 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="?p={R:1}" />
</rule>
But then i can't do www.domain.com/contact/send/.
If I create a rule to rewrite www.domain.com/contact/send/ I get in web.config:
<rule name="RewriteUserFriendlyURL1" 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="?p={R:1}&a={R:2}" />
</rule>
But then I can't do www.domain.com/contact/
Both of the rules do so that I can't see any scripts, css og images from my /scripts/, /css/ and /images/ folders.
How do I make a rule to match both AND to NOT match the 3 folders mentioned above?

Your rule could be something like this (I have expanded and commented it for an easier understanding and eventual editing):
^
(
(?!css|scripts|images) # The directories you don't want to match
[^/]+ # The matched directory
)
(
/
(
([^/]+) # Optional part
/? # Optional trailing slash
)?
)?
$
Which translates into the following:
<match url="^((?!css|scripts|images)[^/]+)(/(([^/]+)/?)?)?$" />
The rewrite url should then be updated to: ?p={R:1}&a={R:4} because of the changes in the number of captures of the new regular expression.

Related

IIS URL rewrite not work for remove trailing slash

I need to add "Remove trailing slash" if this slash is 2 or more.
Using this Regex pattern (.*)/{2,}$ works in test pattern but does not work on live environment.
For example:
https://test.com/ => https://test.com/
https://test.com// => https://test.com/
https://test.com////// => https://test.com/
There is a problem with the parameters you are using, you can try this rule:
<rule name="test" stopProcessing="true">
<match url="^(.*?)//+(.*)$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Redirect" url="{R:1}/" redirectType="Temporary" />
</rule>

IIS URL rewrite conflict between files and urls

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>

Simple URL rewrite rule for IIS

I need a simple URL rewrite rule for IIS. If the URL doesn't include "/i" after the domain, I would like to add it.
For Example:
If www.abc.com/sample/test.aspx
I need the rule to change it to:
www.abc.com/i/sample/test.aspx
Your rule should be like that:
<rule name="prepend i">
<match url=".*" />
<conditions>
<add input="{REQUEST_URI}" pattern="^/i/" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Redirect" url="/i/{R:0}" />
</rule>

URL Rewrite module for IIS7 Not Handling Rewrite Rule Correctly

I have the attached redirect and rewrite rules. Essentially, www.foo.com/vehicles/vehicle-detail.aspx?stockno=123456 is rewritten to www.foo.com/123456. This works as expected with the rules I have in place. Now, if I attempt to browse to a page that does not exist (i.e. www.foo.com/test.aspx) the rewrite rule qualifies and IIS attempts to forward the request to www.foo.com/vehicles/vehicle-detail.aspx (without the querystring). On the vehicle-detail.aspx page I have code to redirect to the homepage if there isnt a valid stockno in the querystring, so this is what is happening. I am not sure how to correct the rule(s), so that the condition does not qualify as true. I have attached rules and screenshots of Failed Request Tracing I enabled to watch the request/rewrite rule.
<rule name="Redirect StockNo" enabled="true" stopProcessing="true">
<match url="^Vehicles/Vehicle-Detail\.aspx$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^stockno=([^=&]+)$" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="false" />
<rule name="Rewrite StockNo" enabled="true" stopProcessing="true">
<match url="^([^/]+)/?$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="Vehicles/Vehicle-Detail.aspx?stockno={R:1}" />
What you need is something more unique for the friendly pattern. If you know that it's always going to be digits, you can have:
<match url="^([\d]+)/?$" />
This will only match to www.foo.com/{digits} with an optional trailing slash.
Another option is to make the url something like www.foo.com/s/{stockno}/. With the /s/ you have something unique that you can confidently match. S is just an example that is short for stockno, so you can use what you want.

IIS URL Rewrite - match pattern

I'm trying to rewrite the path folder1/folder2/folder3...
to d
efault.aspx?id=folder1/folder2/folder3
(default.aspx?id={R:1}/")
I currently have the following pattern: ^([^/]+/?[^/]+/?[^/]+/?[^/]+)/$ which works ok with a 4-level deep folder structure. However, since the number of folders should vary, I'm wondering if there is a pattern that does not limit me to 'hardcode' to a number of folders in the URL.
thanks
This probably isn't exactly what you are looking for, but based on your example will work...
<rule name="ExampleRule" stopProcessing="true">
<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="/default.aspx?id={R:1}" />
</rule>

Resources