Customize HTTP header in IIS URL Rewrite module - asp.net

I was stuck by a simple outbound rule, I want to modify the HTTP Content-Type to application/atom+xml, if the URL exactly matches http://wayneye.com/Feeds/Atom
My rule XML:
<outboundRules>
<rule name="AtomFeedsIMEType" patternSyntax="ExactMatch">
<match serverVariable="RESPONSE_CONTENT_TYPE" pattern="http://{HTTP_HOST}/Feeds/Atom" />
<action type="Rewrite" value="application/atom+xml" />
</rule>
Need help...

You are matching the server variable against the full URL, including domain name. That's not going to work ;-). It doesn't really matter what the value of the Content-Type is, you're going to replace it anyway so you can match is against anything. To make sure you don't replace it on every page, you need to add a precondition to match only requests starting with /Feeds/Atom (on {REQUEST_URI} ). Here's an example:
<outboundRules>
<rule name="AtomFeedsIMEType" preCondition="Match atom feeds">
<match serverVariable="RESPONSE_Content_Type" pattern="(.*)" negate="false" />
<action type="Rewrite" value="application/atom+xml" replace="true" />
</rule>
<preConditions>
<preCondition name="Match atom feeds">
<add input="{REQUEST_URI}" pattern="^/Feeds/Atom" />
</preCondition>
</preConditions>
</outboundRules>
For this to work, the server has to be set up to allow changing of the Content-Type header. This can be done either on the server level or on the site level but needs to be done by the Administrator. It's set in the applicationHost.config and not in the web.config. Here is a part of the applicationHost.config that allows that:
<location path="your_site_name">
<system.webServer>
<rewrite>
<allowedServerVariables>
<add name="CONTENT_TYPE" />
</allowedServerVariables>
</rewrite>
</system.webServer>
</location>
You can also allow this from the GUI, with the View Server Variables link under actions from the main URLRewrite screen. Hope this helps.

Related

Bulk adding 301 redirects to URLRewrite using rewriteMaps list

I'm trying to bulk add a list of external domains that are pointed at my server and add a 301 redirect to point them to specific pages on my site. I've set up a URLRewrite rule in IIS and set up rewriteMaps in the web config with the old domain and the new page. What I have so far not only doesn't work, but it crashes the CSS on the page when the rule is enabled. Where am I going wrong here??
Here is the URLrewrite rule. What I really want it to do is look through the rewriteMap list and redirect from there. I have this as the first rule:
<rule name="Old site redirects" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="true">
<add input="{OldDomainList:{HTTP_HOST}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="false" redirectType="Permanent" />
</rule>
and here is the rewriteMaps code:
<rewriteMaps>
<rewriteMap name="OldDomainList">
<add key="myold_domain1.com" value="https://www.my_newdomain.com/sub1/area1/page" />
<add key="a_different_domain.com" value="https://www.my_newdomain.com/sub2/area5/region4/page" />
</rewriteMap>
</rewriteMaps>
When I enable the rule, the site slows down massively, and when I visit either of those old domains, all that happens is it gets redirected to HTTPS, which is a different URLrewrite rule that I have further down the list, so it is missing this rule out. I'd say that means my rewrite rule input is wrong, but not sure where

Redirect Rule in web.config not including folder path

I'm working on a client application where they have a nested HTML-only app in a rooted subfolder of an otherwise ASP.NET application.
The domain name is changing for the HTML app only, and I need to be sure that the requests are routed to the new domain name. So, using the rewriteRules sounds like a perfect tool for this, but I'm doing something wrong and need another set of eyes.
The original URL is something like this:
https://example.com/folder1/folder2/app/index.html
The new domain is on an entirely different server and I want all requests from the app folder to go to the new domain name. Here is an example:
https://newdomain.com/folder1/folder2/app/index.html
Unfortunately, I'm doing something wrong that's probably obvious... I keep getting this URL in the redirect.
https://newdomain.com/index.html
Here is the web.config showing what I tried. It's in the app folder.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<customErrors mode="Off" />
</system.web>
<system.webServer>
<rewrite>
<rules>
<rule name="RedirectHtmlApp" stopProcessing="true">
<match url="^(.*)$" ignoreCase="true" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^(www\.)?example\.com$" />
</conditions>
<action type="Redirect" url="https://newdomain.com/{R:0}" redirectType="Permanent" appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
{R:0} and {R:1} both return index.html.
What is passed to {R:0} is relative. So if your web.config is in /folder1/folder2/app/{here} then "folder1/folder2/app/" are not passed along. If your web.config rule is in the root of the site-app above /folder1, then I am wrong, but if I am right, then you just need to include the path to the root of the app at the destination also. Given what you have above,
<action type="Redirect"
url="https://newdomain.com/folder1/folder2/app/{R:0}"
redirectType="Permanent" appendQueryString="true" />

iis rewrite outbound rule FilterByTags not working

I've got an outbound rule as shown below:
<outboundRules>
<rule name="ChangeReferencesToOriginalUrl" patternSyntax="ExactMatch" preCondition="CheckContentType">
<match filterByTags="None" pattern="http://oldDomain.com" />
<action type="Rewrite" value="http://newDomain.com/blog" />
</rule>
<preConditions>
<preCondition name="CheckContentType">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^(text/html|text/plain|text/xml|application/rss\+xml)" />
</preCondition>
</preConditions>
</outboundRules>
When the filterByTags value is set to "None", the scheme works as expected - all instances of the old URL are replaced. Of course, this is clumsier than using just the "A" or "Link" tags. However, when I specify those tags as shown in the documentation (https://www.iis.net/learn/extensions/url-rewrite-module/url-rewrite-module-20-configuration-reference#Tag_Filters), the rewrite scheme is ignored and no URLs are replaced.
I'm on an Azure web app, and I think I'm logging FRT properly. It doesn't even record a file for this event. For the sake of testing, I can definitely make it log a file by screwing up the rules :)
What gives? How does one debug this kind of problem?

IIS redirect from subdomain to path

Suppose i have real subdomain like this.
test.example.com, is it possible to write a rule for iis to redirect subdomain to test.example.com/SomeStaticPage.html
the rule should work for multi level subdomains, ie.
test.aaa.bbb.example.com should be redirected to
test.aaa.bbb.example.com/SomeStaticPage.html
Using URL Rewrite is very easy, in fact I'm wondering if I don't get the requirements exactly. (do you need different static pages per domain?, or is it really just to redirect to the same "SomestaticPage.html" while keeping the host name when no default page is specified?)
A simple rule like this would do what your question asks:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="RedirectToSomeStatic" stopProcessing="true">
<match url="^$" />
<action type="Redirect" url="/SomeStaticPage.html" />
</rule>
</rules>
</rewrite>
<system.webServer>
</configuration>
Now, if you were looking for a more complete solution that allows you to specify a page based on the host name, then you could use a rewrite map where you add the host names that should be redirected and which page they should go to. For example the rule below will check if they are requesting "/" and the host name is in the rewrite map and will use that page to redirect, if it is not in the list then it will not do anything:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="RedirectToSomeStatic" stopProcessing="true">
<match url="^$" />
<action type="Redirect" url="{C:0}" />
<conditions>
<add input="{HostMap:{HTTP_HOST}}" pattern=".+" />
</conditions>
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="HostMap">
<add key="test.example.com" value="/SomeStaticForTestExample.html" />
<add key="test.a.b.c.example.com" value="/SomePageForTestABC.html" />
</rewriteMap>
</rewriteMaps>
</rewrite>
</system.webServer>

Remove Trailing Slash From the URL

I want to redirect "abc.aspx/" to "abc.aspx". How can this be done?
Page gets broken when requested page ends with '/'. How to handle such type of requests?
Is there any rewriting rule that can be added into web.config file?
In your web.config, under the system.webServer, add
<rewrite>
<rules>
<!--To always remove trailing slash from the URL-->
<rule name="Remove trailing slash" stopProcessing="true">
<match url="(.*)/$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="{R:1}" />
</rule>
</rules>
</rewrite>
Some Gotchas
In your development environment, if you run your web site under Visual Studio Development Sever, you won't be able to see this feature working. You will need to configure your application to run under at least IIS Express.
When you deploy your web site and see that this feature is not working on your production server, it will be because you miss configured something. One of the common mistakes is having overrideModeDefault attribute set to Deny for rules under <sectionGroup name="rewrite"> inside your applicationHost.config file.
If you are on a shared hosting environment and you see that this feature is not working, ask your provider if they have given you the permission of configuring this part.
Source: http://www.tugberkugurlu.com/archive/remove-trailing-slash-from-the-urls-of-your-asp-net-web-site-with-iis-7-url-rewrite-module
I know this is an old post, but Mihai's answer can make your application vulnerable to open redirects attack (see this)
<action type="Redirect" redirectType="Permanent" url="{R:1}" />
This redirect URL can be used for an external redirect, so make sure you have other validations in place before this rule is evaluated or change the redirect to make it internal instead. If you really want to remove the trailing slash and keep it internal, you can change it to:
<action type="Redirect" redirectType="Permanent" url="/{R:1}" />
Cheers

Resources