How do I create a redirect URL in IIS 7.5 - 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>

Related

IIS rewrite rule not triggered when file-name plus extension is in the url

I am struggling getting an IIS rewrite rule working.
The old urls look like:
https://wwww.testserver.com/package.aspx?TrackingNumber=number
The new web app is expecting the urls in this format:
https://wwww.testserver.com/package/number
Currently I have the following rule setup, which is not working.
<rule name="Rewrite to new Package site" stopProcessing="true">
<match url="^package\.aspx\?TrackingNumber=([_0-9a-z-]+)" />
<action type="Redirect" url="https://www.testserver.com/package/{R:1}" logRewrittenUrl="true" />
</rule>
The interesting part is, if I remove package.aspx\? from the rule, urls like these https://wwww.testserver.com/oldwebsite/TrackingNumber=number are getting matched.
In my test calls I replaced package.aspx with package.html and these urls are not getting matched as well. It looks like IIS ignores urls with filenames in the url.
You could use below url rewrite rule to redirect https://wwww.testserver.com/package.aspx?TrackingNumber=number to https://wwww.testserver.com/package/number:
<rule name="test" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{REQUEST_FILENAME}" pattern="package(.aspx)" />
<add input="{QUERY_STRING}" pattern="TrackingNumber=(.*)" />
</conditions>
<action type="Redirect" url="http://localhost:2568/package/{C:1}" appendQueryString="false" />
</rule>
Note: You could modify hostname as per your requirement.
Regards,
Jalpa

IIS URL Rewrite rule to replace part of the URL

I am new to the IIS Rewrite rule and trying to create a rule to replace part of url
e.g.www.abc.com/assets/global/xyz.jpg
should redirect to www.abc.com**/en/globalassets/**assets/global/xyz.jpg
I fiddle around with the following rule but no success
<rule name="url replace">
<match url="^(.com/assets/global/)" />
<action type="Rewrite" url=".com/en/globalassets/assets/global/{R:2}" />
</rule>
According to your description, I have tested on my side , you could use urlrewite rule as below:
<rule name="rule1" enabled="true" stopProcessing="true">
<match url="assets/global/(.*)" />
<conditions>
<add input="{REQUEST_URI}" pattern="en/globalassets" negate="true" />
</conditions>
<action type="Redirect" url="http://{domain}/en/globalassets/assets/global/{R:1}" />
</rule>
Firstly, we couldn't add value like **.com in match url, because this part could only catch path of the url.
You could see this is a url structure:
http(s)://httphost/path?querystring.
You could only get it in conditions tag but not pattern.
Then you should add condition to check the request URL match "en/globalassets" or not to avoid running redirect rule once and once again.

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.

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>

Setting up URL Rewrite rule for a specific domain

For local dev testing, I need to catch all requests to www.somedomain.com/XXX (where X is the path), and send them on to localhost/somevdir/XXX.
I've added this to my HOSTS file (c:\windows\system32\drivers\etc):
127.0.0.1 www.mydomain.com
Then, in IIS8 (Windows 8), I've added a binding to my "Default Web Site" for the host www.mydomain.com. This works, I can now browse www.mydomain.com/test.html and see a test html page. My virtual dir is inside the Default web site. I then add a URL Rewrite URL to the web site for the last bit:
<rewrite>
<rules>
<rule name="mydomain.com" stopProcessing="true">
<match url="^www.mydomain.com/(.*)" />
<action type="Rewrite" url="localhost/MyVDir/{R:1}" logRewrittenUrl="true" />
</rule>
</rules>
</rewrite>
But - that doesn't work. I get a 404 so it looks the the match never happens. I've tried redirect and rewrite, and I've tried without the ^ in the regex and a few other regex tweaks. Can someone explain what I've done wrong?
I think the following should work:
<rule name="mydomain.com" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^(mydomain\.com|www\.mydomain\.com)$" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="http://localhost/MyVDir/{R:1}" redirectType="Temporary" />
</rule>
The match on any URL makes sure the conditions are checked, and the HTTP_HOST server variable seems the most reliable way of checking the requested hostname. You could remove the REQUEST_FILENAME input condition, but it works as quite a nice sanity check to make sure static files are always served.
The following is better for catching both www. and non-www. versions of the domain so that you don't have to write the domain twice, which could possibly cause errors with being twice as likely to write a typo. (The parenthesis with the ? character means optional in regex terms.)
<rule name="mydomain.com" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^(www.)?mydomain\.com$" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="http://localhost/MyVDir/{R:1}" redirectType="Temporary" />
</rule>

Resources