Simple URL rewrite rule for IIS - 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>

Related

Rewrite rule for a folder/page name without an extension

I have a rewrite rule that doesn't seem to be working as I anticipated. I'm redirected to a 404. It seems to only work if I have an extension on the page.
/folder/page.html works but /folder/page doesn't.
<rule name="preact" 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}" pattern="^/(docs)" negate="true" />
</conditions>
<action type="Rewrite" url="index.html" />
</rule>

URL Rewrite - Remove End of URL If Matches Specific String

Using URL rewrite and we have lots of old URLs which take the form
/something/else/index.cfm
/this/that/index.cfm
What I would like to do is remove the /index.cfm from these examples
Any ideas?
Thanks
<rule name="Remove Trailing Index.cfm" enabled="true">
<match url="^(.*)/index.cfm" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{R:1}" pattern="category" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Redirect" url="/{R:1}" />
</rule>

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 rewrite rule for processing all URLs except given

Here is the IIS rewriting rule for some URLs to process:
<rule name="rule" stopProcessing="true">
<match url="^(word|world|hero|about)[/]?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/info.aspx?id={R:1}" />
</rule>
How can I create an opposite rule? For example, I want to process all URLs except "/special", "/escape". This:
<rule name="rule" stopProcessing="true">
<match url="^(?!(special)&?!(escape))[/]?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/info.aspx?id={R:1}" />
</rule>
does not work. "/special" and "/escape" URLs are processed as they should, but other URLs give me 404 pages.
You have to use the negate attribute in this case.
Your rule will become:
<rule name="rule" stopProcessing="true">
<match url="^(special|escape)/?$" negate="true" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/info.aspx?id={R:1}" />
</rule>

Specific Redirect/Rewrite Rule for web.config

Can someone assist with a redirect rule?
I would like to swap any occurrence of, say, "newjersey", anywhere in the url for "new-jersey".
Any suggestions on how to do this via web.config would be great, as I need to do this right away. TIA!
Got it working via the following:
<rule name="Redirect {1}/newjersey" stopProcessing="true">
<match url="^([^/]+)/newjersey$" ignoreCase="false" />
<conditions trackAllCaptures="true">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Redirect" url="/{R:1}/new-jersey" appendQueryString="false" />

Resources