IIS URL Rewrite - match pattern - iis

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>

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.

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>

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" />

II7 rewrite rule for (htm|html|asp) to php if file doesn't exist

I have an old website done using classic ASP and also HTM/HTML files.
Now I want to rename the files to .php but keep the url as .asp, or .html
www.example.com/some/page.htm to www.example.com/some/page.php
www.example.com/another_page.asp to www.example.com/another_page.php
and so on..
I have tried this one inspired by another answer found here:
<rule name="HTML to PHP">
<match url="^(.*)\.htm$" ignoreCase="true" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="{R:2}.php" />
</rule>
But I don't know if it works because I have to contact my hosting to apply the changes I've done.
Change your action tag to:
<action type="Rewrite" url="/{R:1}.php" />
And you might want to consider adding the query string as well (if provided) with:
<action type="Rewrite" url="/{R:1}.php" appendQueryString="true" />

Resources