IIS URL rewrite to access subfolder content - iis

Is it possible to create a URL rewrite that places the content of a subfolder in the root?
Example:
/pages/article1 would be reached from /article1
/pages/otherstuff would be reached from /otherstuff
I don't want to use virtual directories, because we're talking about hundreds of static pages in the /pages folder.

I found the answer myself... This did the trick:
<rule name="RemoveSEOFolder" stopProcessing="true">
<match url="^seo$|^seo/(.*)$" />
<conditions>
</conditions>
<action type="Redirect" url="seo/{R:1}" />
</rule>
<rule name="RewriteToFile">
<match url="^(?!seo/)(.*)" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="seo/{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.

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 user-friendly urls multiple rules conflict

I've been trying to create user-friendly urls for two of my cfm pages using url rewrite module on IIS 8. That said, there seems to be some sort of conflict due to similarity between rules or something.
Here is what I want to achieve. The following pages:
www.mywebsite.com/news.cfm?category=microsoft
www.mywebsite.com/news.cfm?category=apple
etc
www.mywebsite.com/blog.cfm?title=sometitle
www.mywebsite.com/blog.cfm?title=sometitle
etc
Should look like this:
www.mywebsite.com/news/microsoft
or this
www.mywebsite.com/microsoft
www.mywebsite.com/blog/sometitle
or this
www.mywebsite.com/sometitle
You get the picture.
Here is my code:
<rule name="RedirectUserFriendlyURL4" stopProcessing="true">
<match url="^news\.cfm$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^category=([^=&]+)$" />
</conditions>
<action type="Redirect" url="news/{C:1}" appendQueryString="false" />
</rule>
<rule name="RewriteUserFriendlyURL4" stopProcessing="true">
<match url="^news/([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="news.cfm?category={R:1}" />
</rule>
<rule name="RedirectUserFriendlyURL5" stopProcessing="true">
<match url="^blog\.cfm$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^title=([^=&]+)$" />
</conditions>
<action type="Redirect" url="blog/{C:1}" appendQueryString="false" />
</rule>
<rule name="RewriteUserFriendlyURL5" stopProcessing="true">
<match url="^blog/([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="blog.cfm?title={R:1}" />
</rule>
The rules above do not work together and both pages redirect to the one that's set in the first rule. Can this be done with a single rule or do I need to use two? How can I sort this out? I would really appreciate your help on this one. I am using ColdFusion 10 on Windows and IIS 8.

Friendly URL with the same pattern

I can redirect and make a single Friendly URL:
<rule name="RedirectUserFriendlyURL1" stopProcessing="true">
<match url="^Product/Tour\.aspx$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
</conditions>
<action type="Redirect" url="Product/Tour" appendQueryString="false" />
</rule>
<rule name="RewriteUserFriendlyURL2" stopProcessing="true">
<match url="^Product/Tour$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="Product/Tour.aspx" />
</rule>
But, for example, if I have:
http://www.domain.com/Product/Features.aspx
http://www.domain.com/Product/Download.aspx
http://www.domain.com/Product/FAQ.aspx
etc.
Can I write one rule to make friendly URL for all of these links in order to receive?
http://www.domain.com/Product/Features
http://www.domain.com/Product/Download
http://www.domain.com/Product/FAQ
It's easy when there is a couple of links, but with a lot of rules it's hard to maintain.
You can use the regex pattern and the back reference to do this:
<rule name="RedirectUserFriendlyURL1" stopProcessing="true">
<match url="^Product/([A-z0-9]+)\.aspx$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
</conditions>
<action type="Redirect" url="Product/{R:1}" appendQueryString="false" />
</rule>
<rule name="RewriteUserFriendlyURL2" stopProcessing="true">
<match url="^Product/([A-z0-9]+)$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="Product/{R:1}.aspx" />
</rule>
If you have a lot of urls to rewrite, you should read the documentation about the Rewrite Maps as well.

IIS URL Rewrite module: redirect based on query string

I Have some problems with redirecting to another URL based on the query string parameters.
I have url for mapping service, for example "http://www.mydomain.com/?lon=111&lat=222&zoom=3"
and I need it to be redirected to "http://www.mydomain.com/111/222/3".
I have this rule in my web.config, but it does not work
<rule name="Location redirect">
<match url="\?lon=(\d+)&lat=(\d+)&zoom=(\d+)" />
<action type="Redirect" url="{R:1}/{R:2}/{R:3}" redirectType="Found" />
</rule>
The reason is that URL does not include Query String, you ned to use conditions for that. If you use the User Interface, it includes a friendly URL template that will generate it for you.
Below you will find the Rewrite rule as well as the redirect so that legacy traffic (ugly URL) is redirected to the pretty URL:
<rule name="RedirectUserFriendlyURL1" stopProcessing="true">
<match url="^$" />
<conditions>
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
<add input="{QUERY_STRING}" pattern="^lon=([^=&]+)&lat=([^=&]+)&zoom=([^=&]+)$" />
</conditions>
<action type="Redirect" url="/{C:1}/{C:2}/{C:3}" appendQueryString="false" />
</rule>
<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="?lon={R:1}&lat={R:2}&zoom={R:3}" />
</rule>

Resources