IIS 7 Url Rewrite - Matching Sub Domains, but excluding specific urls - iis-7

I have a URL rewrite set up on a site where users can create their own sites on a subdomain.
<rule name="CName to URL - Rewrite" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(?!www)(.*)\.basedomain\.com" />
</conditions>
<action type="Rewrite" url="render_page.aspx?site={C:1}&page={R:0}" />
</rule>
As an example, a user could create a page http://client.basedomain.com/about-us and this will successfully pass "client" and "about-us" to my application.
What I need to be able to do is override this behavior in the case of a specific page name following their domain.
So if the page http://client.basedomain.com/restricted was accessed, it would not perform the above rewrite, and instead rewrite it to the url "render_page_restricted.aspx?site={C:1}
Any help is GREATLY appreciated.

In standard fashion, after spending a few days working on it, then finally posting it on SE, I cracked it 20 min later. Sigh. In the event someone else has the same problem, here's my fix:
<rule name="Restricted Page Name Override - Rewrite" stopProcessing="true">
<match url="^(?!www)(.*)restricted" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(?!www)(.*)\.basedomain\.com"/>
</conditions>
<action type="Rewrite" url="render_page_restricted.asp?site={C:1}" />
</rule>

Related

How can I configure url rewrite to redirect from splash.aspx?page=3 to splash3.aspx?

I have already created a topic about query strings, unfortunately the question was closed and no one answers anymore.
For example, I want to redirect from splash.aspx?page=3 to a simpler url e.g. to splash3.aspx. I unfortunately have no backend code where the query string can respond, so I need an alternative. How can I configure url rewrite?
In your web.config, add the following nodes:
<rewrite>
<rule name="Redirect splash page 3" stopProcessing="true">
<match url="splash\.aspx$" />
<conditions>
<add input="{QUERY_STRING}" pattern="page=3" />
</conditions>
<action type="Redirect" url="splash3.aspx" redirectType="Temporary" />
</rule>
</rewrite>
For more information see https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/url-rewrite-module-configuration-reference

Combine several 301 redirects into one for ASP.NET URL Rewrite module

I am trying to move a complex redirect logic implemented in Global.asax into a set of rules for the IIS URL Rewrite module in a classic ASP.NET website. Shortly, the logic must redirect requests like
{http|https}://[www.]ourdomain.com/[Home/]Products/old-product-name/page.aspx
to
https://ourdomain.com/new-product-name/
(optional and variable parts are in square and curly brackets).
The first 3 main things I implemented with URL Rewrite rules are the followings:
<rule name="Redirect to HTTPS">
<match url=".*" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
</rule>
<rule name="Canonical Host Name">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^ourdomain\.com$" />
</conditions>
<action type="Redirect" url="https://ourdomain.com/{R:1}" redirectType="Permanent" />
</rule>
<rule name="Default Page">
<match url="(.*)default.aspx" />
<conditions>
<add input="{REQUEST_URI}" negate="true" pattern="-default.aspx$" />
</conditions>
<action type="Redirect" url="{R:1}" redirectType="Permanent" />
</rule>
These rules allow to redirect requests like http://www.ourdomain.com/product-name/default.aspx to https://ourdomain.com/product-name.
However, the browser developer tools report that this conversion causes 301 redirects. I tried to find a solution of this redirect chain problem in the Internet and found this post. After reading it, I managed to recode my rules to have just one final 301 redirect with URL rewriting:
<rule name="Redirect to HTTPS">
<match url=".*" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Rewrite" url="_{REQUEST_URI}" redirectType="Permanent" />
</rule>
<rule name="Canonical Host Name">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^ourdomain\.com$" />
</conditions>
<action type="Rewrite" url="_{R:1}" redirectType="Permanent" />
</rule>
<rule name="Default Page">
<match url="(.*)default.aspx" />
<conditions>
<add input="{REQUEST_URI}" negate="true" pattern="-default.aspx$" />
</conditions>
<action type="Rewrite" url="_{R:1}" redirectType="Permanent" />
</rule>
<rule name="FINAL REDIRECT" stopProcessing="true">
<match url="^(_+)(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{HTTP_METHOD}" pattern="GET" />
</conditions>
<action type="Redirect" url="https://ourdomain.com{R:2}" />
</rule>
However, this approach looks non-natural. It makes hard adding new rules because of these workarounds with the underscore character. And maybe, such several rewrite operations for one request may impact on the website performance.
Is there another, more elegant solution, of the 301 redirect chain problem for the URL Rewrite module?
This is not a limitation of IIS Url Rewrite, but the way the web works. A redirection sends a header to the browser telling it that it should navigate to another place. If you have 3 rules that apply to the same original request, and the 3 apply a redirection, then you'll always get 3 redirections no matter how you do it: IIS Url Rewrite, Apache .htaccess or your own custom code.
The solution you've implemented is a "patch" to change several redirections into internal rewrites in the server and a final "catch" of those special rewrites to transform them into a unique redirection at the end (BTW, if you have any real page that strats with _ you won't be able to access it with this extra rule). That's not bad and won't affect your performance, but it's ugly.
I'm not a SEO expert but I think the 301 chaining thing to reach a final similar URL won't hurt your SEO at all in recent times, if that's what worries you (read the final part specially). And being 301 redirects they will affect only the first visit of your users, so probably won't hurt your loading times or conversions either anyway.
However, if you want to avoid that, try to make a single rule for your purpose, doing all the transformations at once. In your case, you want to transform this:
{http|https}://[www.]ourdomain.com/[Home/]Products/old-product-name/page.aspx
(I guess /Home/ can be present sometimes or not)
into this:
https://ourdomain.com/new-product-name/
Then you can use this single rule:
<rule name="New URLs!!" stopProcessing="true">
<match url="^(Home/){0,1}Products/(.+?/).+.aspx" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
<add input="{HTTP_HOST}" negate="true" pattern="^ourdomain\.com$" />
</conditions>
<action type="Redirect" url="https://ourdomain.com/{R:2}" redirectType="Permanent" />
</rule>
and you will transform everything with a single rule (so, just one redirect). The regular expression in the match URL part will identify this specific kind of URL (old product URLs) and the conditions (matching any of them) will take care of the HTTPs and domain change. The stopProcessing="true" will keep other rules to act, and you'll have this specific kind of URLs under control.
Please note that I've not tested this rule and maybe it has minor problems. But you get the idea...
You'll need extra rules after this one to take care of other less-specific URLs, such as your general HTTP to HTTPS or canonical domain rules. But in the case you want to resolve, this will achieve what you want in a single step.

How can I create a URL rewrite rule for a domain-only request?

I am trying to create a URL rewrite for a site that has multiple domains. For example one of the domains is mydomain.com and if a user puts www.mydomain.com in their browser and does not specify a page I want to rewrite the URL to call www.mydomain.com/landingpage.aspx?cat=1&sol=4.
If the user calls anything else such as www.mydomain.com/somepage.aspx this rule should be ignored.
I have installed URL Rewrite 2.0 on the server 2008 R2 machine we have and I have added this rule to the web.config.
<rewrite>
<rules>
<rule name="mydomain.com" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www.)?mydomain.com" />
<add input="{PATH_INFO}" pattern="^$" negate="true" />
</conditions>
<action type="Rewrite" url="\landingpage.aspx?cat=1&sol=4" />
</rule>
</rules>
</rewrite>
I am using the {PATH_INFO} of ^$ so that if anything other than just a call for the domain occurs this should ignore it I think. However it does not work.
I am using .NET 4.0 on the site.
Can someone tell me what I am doing wrong please?
You are looking for following rule:
This will check if URL is empty i.e. no page is specified using match url=^$ - empty string and redirect to specific page.
<rule name="Redirect to specific page" enabled="true" stopProcessing="true">
<match url="^$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www.)?mydomain.com$" />
</conditions>
<action type="Redirect" url="http://{HTTP_HOST}/landingpage.aspx?cat=1&sol=4" />
</rule>

IIS7 URL Rewrite multiple domains to a single domain including google analytics referral code

I am trying to elegantly capture a large number of domains that need to redirect to a single domain for example:
mydomain.com, www.mydomain.com, mydomain-eu.com, mydomain.eu
all to the main domain:
www.mydomain.co.uk
This I can do fairly easily in IIS by creating a site - binding all the domains and doing a redirect.
However there is a catch. The client wants to track where those redirects came from in google analytics.
So they need some parameters passing in the redirect URL, example (using a domain from above):
mydomain.eu
redirects to:
www.mydomain.co.uk?utm_campaign=mydomain.eu&utm_source=mydomain.eu&utm_medium=referral
So - this is a good case to use a URL rewrite. This is what I have in the web.config:
<rule name="Redirect" enabled="false" patternSyntax="Wildcard" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.mydomain.co.uk$" negate="true" />
</conditions>
<action type="Redirect" url="http://www.mydomain.co.uk/?utm_campaign={R:0}&utm_source={R:0}&utm_medium=referral" />
</rule>
If the input does not match the domain I want to be I am redirecting it.
It works perfectly apart from one thing. I just cant get the parameter of the original domain. The {R:0} returns blank. I have tried {C:0} also. I have tested the patterns in the GUI and they tell me I should be getting the requested domain.
What am I doing wrong? Thanks.
This is a guess but what about:
<rule name="Redirect" enabled="false" patternSyntax="Wildcard" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.mydomain.co.uk$" negate="true" />
</conditions>
<action type="Redirect" url="http://www.mydomain.co.uk/?utm_campaign={HTTP_HOST}&utm_source={HTTP_HOST}&utm_medium=referral" />
</rule>
Looked this up and it says you can use it this way in this documentation:
http://www.iis.net/learn/extensions/url-rewrite-module/url-rewrite-module-configuration-reference#UsingServerVars
Thanks #rtpHarry for the guidance. Although the above rule didn't work for me, I managed to tweak it for my IIS7 server.
<rule name="Redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www.tsogosundigishare.com$" negate="true" />
</conditions>
<action type="Redirect" url="http://www.tsogosundigishare.com/" redirectType="SeeOther" />
</rule>
By removing the enabled="false" and patternSyntax from the rule, it converted the rule from being "Wildcard" to "Regular Expression" and adding redirectType="SeeOther" redirected properly.

IIS Url-Rewrite

I'm trying to use IIS7 Url Rewrite module rewrite
services.mydomain.com/some-file-here to mydomain.webhost.com/folder/some-file-here
The rule is as follows:
Pattern = ^services.mydomain.com/(.*)$
Action = Rewrite
Rewrite URL = http://mydomain.webhost.com/folder/{R:1}
The problem is IIS keeps giving me 404 not found errors. I've been stuck at this for days now. Any ideas?
You have your pattern wrong. It should not include domain name or query string there -- only path without leading slash. See the working rule below:
<rule name="MyRewriteRule" stopProcessing="true">
<match url="^(some-file-here)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^services\.mydomain\.com$" />
</conditions>
<action type="Redirect" url="http://mydomain.webhost.com/folder/{R:1}" />
</rule>
The above rule will only trigger if host name is services.mydomain.com. If you do not require such additional condition (which is optional) then just remove these 3 lines: <conditions>...</conditions>
Also, the above rule will only do one specific redirect from services.mydomain.com/some-file-here to mydomain.webhost.com/folder/some-file-here. If you need to redirect ANY file like that, then use this one instead:
<rule name="MyRewriteRule" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^services\.mydomain\.com$" />
</conditions>
<action type="Redirect" url="http://mydomain.webhost.com/folder/{R:1}" />
</rule>

Resources