IIS 8 - Domain and URL rewrite - iis

I am trying to rewrite domain and URL of request so when user goes to URL:
http://somedomain.com/
he sees content of:
https://serverdomain.com/applications/somedomain/
Of course, anything that is appended to somedomain.com/ should also be appended to applications/somedomain/
I installed ARR 3 and Failed Request Logging, but I can't get to this result. Moreover, after installing ARR, 404 errors start throwing 502.2.
The best rule I got was:
<rule name="test-domain" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="somedomain.com" />
</conditions>
<action type="Rewrite" url="https://serverdomain.com/applications/somedomain{R:0}" logRewrittenUrl="true" />
</rule>
It almost works, but I don't know why it takes me to home page (e.g. https://serverdomain.com/).
When I set action type to Redirect it looks OK, but I need this rewrited, so user sees original URL.

Related

How do I create a redirect URL in IIS 7.5

I'm new to the world of IIS in terms of redirects and have been tasked with setting up a redirect for a portion of our site.
The URL I need to redirect is brandview.auditedmedia.com and I need it
to go to https://auditedmedia.com/data/media-intelligence-center/brand-view
The trick is we can have a URL with a QueryString such as
brandview.auditedmedia.com/memberNumber=423524 and that URL should NOT
be redirected.
Also, the scheme of brandview.auditedmedia.com does not matter. Both
HTTP and HTTPS should redirect.
So I'm not sure should I be doing this in IIS or as a RouteMap in the app?
If I should be doing this in IIS can someone point me to either an article or give an example of what I need to do?
Thanks
Bob
You need to add URL rewrite rule on the server level. Use conditions to prevent redirection for requests with non-empty query string string.
For your case it is necessary to add the following rule:
<rule name="brandview redirect" stopProcessing="true">
<match url=".*" ignoreCase="true" negate="false" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^brandview\.auditedmedia\.com$" negate="false" />
<add input="{QUERY_STRING}" pattern=".+" negate="true" />
</conditions>
<action type="Redirect" url="https://auditedmedia.com/data/media-intelligence-center/brand-view" appendQueryString="false" redirectType="Permanent" />
</rule>
There is detailed instruction on your question. Note, the rule must be created in system.webServer/rewrite/globalRules section of %windir%\system32\inetsrv\config\ApplicationHost.config file.
I got the pattern fixed (see below) and though all the pattern matching works properly in URL Rewrite Module (using test button and putting in different URLs), it still isn't working properly when I attempt to go to the site. The server just continues with serving the page and not redirecting.
<rewrite>
<globalRules>
<rule name="brandview redirect" stopProcessing="true">
<match url=".*" ignoreCase="true" negate="false" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^(https?:\/\/)?brandviewdev\.auditedmedia\.com$" negate="false" />
<add input="{QUERY_STRING}" pattern=".+" negate="true" />
</conditions>
<action type="Redirect" url="https://auditedmedia.com/data/media-intelligence-center/brand-view" redirectType="Permanent" appendQueryString="false" />
</rule>
</globalRules>
</rewrite>
Also you can add URL rewrite rule using GUI. Before you start make sure that both of the direct HTTP-requests to brandview.auditedmedia.com and https://auditedmedia.com/data/media-intelligence-center/brand-view returns status 200 (OK).
Open IIS Manager by prompting inetmgr command.
Select brandview web site in the Connections pane and open URL Rewriting feature.
Create new empty rule using Add rule(s)... action.
Within the Edit Inbound Rule dialog, enter the following:
Name: Redirect to auditedmedia.com;
Requested URL: Matches the Pattern;
Using: Regular expressions;
Pattern: .*;
Action type: Redirect;
Redirect URL: https://auditedmedia.com/data/media-intelligence-center/brand-view.
Append query string: false;
Redirect type: See Other (303);
Save the rule by clicking Apply on the Actions pane.
Make sure that request to brandview.auditedmedia.com will be redirected to https://auditedmedia.com/data/media-intelligence-center/brand-view.
Repeat steps 1, 2 and select Redirect to auditedmedia.com rule.
Execute action Add condition(s)... with following:
Condition input: {QUERY_STRING};
Check if input string: Does Not Matches the Pattern;
Pattern: .+;
Ignore case: true.
Push OK button.
As result, all requests to brandview.auditedmedia.com with empty query string will be redirected to https://auditedmedia.com/data/media-intelligence-center/brand-view page. After testing you can change redirect type to 301.
After trying different combinations I finally got the redirect to work correctly and to be honest, I'm not sure how. Here is my config
<rewrite>
<rules>
<rule name="Brandview Redirect" enabled="true" stopProcessing="true">
<match url="^$" ignoreCase="true" negate="false" />
<action type="Redirect" url="https://auditedmedia.com/data/media-intelligence-center/brand-view" appendQueryString="false" redirectType="Temporary" />
<conditions>
<add input="{HTTP_HOST}" pattern="brandviewdev\.auditedmedia\.com" />
<add input="{QUERY_STRING}" pattern=".+" negate="true" />
</conditions>
</rule>
</rules>
</rewrite>

How to pass two URL parameters with IIS 8.5 URL ReWrite module?

I am trying to pass two parameters in the URL with the following url rewrite rule and the page keeps showing 404 error.
Actual URL: https://devbox.mysite.com/kb/article/?slug=view-my-class-schedul&role=56c4cfe091121c0b5b47fe66
I want to show URL on browser: https://devbox.mysite.com/kb/article/view-my-class-schedule
Current URL Rewrite rule that's not working:
<rule name="KB-rewrite" enabled="true">
<match url="^kb/article/([a-zA-Z0-9\-]+)&role=([a-zA-Z0-9\-]+)$" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{QUERY_STRING}" pattern="1" negate="true" />
</conditions>
<action type="Rewrite" url="kb/article/?slug={R:1}&role={R:2}" appendQueryString="false" />
</rule>
Okay, I think I figured it out. The display URL has to be something like: https://devbox.mysite.com/kb/article/view-my-class-schedule/reader. I guess the way I was thinking is not possible.

Redirect works but rewrite doesn't on an Azure web app

I am trying to redirect from my old ISP to my azure web site. I have set up the necessary DNS records. As I am using sub-folders on my Web App I need to set up redirection rules in the root web.config file to point from the domain name to the correct sub-folder of the main site.
It works perfectly when I have a redirect rule.
<rule name="Works" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^mysite.com$" />
</conditions>
<action type="Redirect" url="http://mysite.azurewebsites.net/mysub"/>
</rule>
However when I change to a Rewrite it fails. I really want a rewrite as I don't want the user to see the change of url in the browser.
<rule name="Fails" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^mysite.com$" />
</conditions>
<action type="Rewrite" url="http://mysite.azurewebsites.net/mysub"/>
</rule>
I get the following message:
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
What am I doing wrong?
Your rewrite action should be a relative path, see the documentation:
http://www.iis.net/learn/extensions/url-rewrite-module/url-rewrite-module-configuration-reference#Rewrite_action
A substitution string must always specify the URL path

IIS Rewrite Rule in web.config to redirect HTTPS requests to HTTP

I need to redirect all https requests to http, for example, if someone visits https://www.example.com/another-page/ to http://www.example.com/another-page/
I have the following rewrite rule in my web.config right now, but it's not working correctly. It's redirecting https://www.example.com/another-page/ to https://www.example.com/, so to the root of the site, but instead I want the redirect to stay in the same URL and only rewrite https to http.
<rule name="Redirect to HTTP" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{R:1}" pattern="^onepage/(.*)$" negate="true" />
<add input="{HTTPS}" pattern="^ON$" />
</conditions>
<action type="Redirect" url="http://{HTTP_HOST}/{R:0}" redirectType="Permanent" />
</rule>
Any help on changing the above rule so that it only changes https to http, but keeps the full url visited would be greatly appreciated!
I set up your rule, cleaned up a little, and it worked; so this isn't really answering with much new.
Suggestion: Remove the onepage input condition just for testing, as cheesmacfly suggested in the question comment.
Also, try changing the action to {R:1} instead of {R:0}. It shouldn't matter in this case, but I just like using 1 and up, to match the specific capturing group. R:0 means the entire matched string, which always confuses me just a little.
<rule name="Redirect to HTTP" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^ON$" />
</conditions>
<action type="Redirect" url="http://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
One possibility is that your browser has cached a previous attempt of your rules. When the redirectType is Permanent, and you're still developing or testing, the browser often caches a previous rule. Clear your browser cache, and/or remove the Permanent, and/or browse in incognito mode. When done testing, change it to permanent. See number 2 and 3 in this answer: https://stackoverflow.com/a/9204355/292060
Please paste the below code in web.config file.
<rule name="Redirect to http" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="http://{HTTPS_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
</rule>

url rewrite - remove subdomain only when directory meets criteria

I have seen a handful of similar questions on handling removing the www from a URL, but I was needing to remove it only when a certain directory exists. The real issue is that I run the "test" in URL Rewrite within IIS and it works fine, but for some reason does not respond when I type in the URL. I have tried switching to when "not matches" and it redirects, so I am a bit baffled what I am doing wrong to get this to work.
An example URL would be http://www3.test.com/feed/testing.xml
If the directory "feed" exists in the URL then remove the www3. I thought this would be fairly simple.
<rule name="fix feeds" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="(www3\.)(.*)(feed/.*)" />
</conditions>
<action type="Redirect" url="http://{C:2}{C:3}" appendQueryString="false" redirectType="Permanent" />
Another issue I am finding is that if you set it to permanently redirect 301, then once you get something to fire (and may not work as expected) the browser caches the 301 redirect and makes a mess of your testing environment.
You can't match feed in HTTP_HOST instead you need to use Match URL.
You have to match /feed/testing.xml in match URL which will yield here R:0
Match your patter if it has www3 and take C:2 which will give you URL without www3.
<rule name="fix feeds" enabled="true" stopProcessing="true">
<match url="^feed/.*" />
<conditions>
<add input="{HTTP_HOST}" pattern="(www3\.)(.*)" />
</conditions>
<action type="Redirect" url="http://{C:2}/{R:0}" appendQueryString="false" redirectType="Permanent" />
</rule>

Resources