I have multiple domains pointing to one Web application. Here are a few, just as an examples, so that I can reference them in my examples.
www.mydomain.com
sub.mydomain.com
www.affiliatedomain.com
ASP.NET allows for a web.config setting to accomplish this:
<httpCookies httpOnlyCookies="true" domain=".mydomain.com" />
This allows me to share sessions between the www.mydomain.com and mydomain.com domains. Perfect! Except it only works for one domain.
Another avenue I explored was url rewriting. I could insert a rewrite rule to ensure that only www urls are being accessed but doing so (at least the one I write) works for the domains, but appends a www. to the sub.mydomain.com resulting in a failed www.sub.mydomain.com - which won't work. Perhaps this option could still work if someone knows how to formulate url rewrite urls better than me.
Finally, to be clear, I don't need to share sessions across all domains (I know this is not possible). I just would like to share domains across the same domain with and without www.
For example: www.mydomain.com and mydomain.com
Or www.affiliatedomain.com and affiliatedomain.com
The sub.mydomain.com can live on its own so long as it never has a www. in front of it.
I think the rewrite option is best for your case, something like this should fix the sub domain rewrite issue.
<rewrite>
<rules>
<rule name="Redirect domain.com to www" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^domain.com$" />
</conditions>
<action type="Redirect" url="http://www.domain.com/{R:0}" />
</rule>
</rules>
</rewrite>
Related
I am wondering if it is possible to set up a rewrite rule where you do not specify the domain, only the page accessed and the page to redirect to. My goal for this is to not have to have different versions of the same rule for different sites (i.e. dev.mysite.com, qa.mysite.com, etc), so one rule works for multiple subdomains. For example, I need to redirect someone going to dev.mysite.com/mexico to mx.dev.mysite.com/es/mexico, but that also has to work for QA. Additionally, production has a slightly different domain which would make this even more difficult. This is what I came up with - it definitely needs work:
<rule name="Test Mexico Redirect">
<match url="^mexico"/>
<action type="Redirect" url="http://mx.{R:2}/es/mexico" redirectType="Permanent"/>
</rule>
Unfortunately it just does not work; I get a blank page. Probably because {R:2} is looking for the domain in the rule and one isn't specified. I can't think of a way to do this, because not even the domain is the same across all environments.
It would be cool if you could use regex in the <action> element, then I would do something like this: ^(mx\.).*com/es/mexico.
It's possible using {HTTP_HOST} and back references like {C:#}. The IIS Rewrite module is extremely useful for setting this up. Here is a screenshot with some notes that explains how it's done:
By using {HTTP_HOST} and conditions I was able to make a rewrite rule that can apply to all environments. The actual XML result:
<rule name="Mexico Redirect" stopProcessing="true">
<match url="mexico" />
<conditions>
<add input="{HTTP_HOST}" pattern="^((?!mx))" />
<add input="{CACHE_URL}" pattern="^(.+)://" />
</conditions>
<action type="Redirect" url="{C:1}://mx.{HTTP_HOST}/es/search" redirectType="Temporary" />
</rule>
Also I may have been incorrect saying you won't need to recycle the app pool with a Temporary (307) redirect. I still needed to recycle it but only in production... no verdict on this yet.
So we have a client whereby we are hosting their site but with 2 different domains mapped (we have bindings for both domains) to the same site in IIS. this is primarily because when they started with us they could only purchase the domain they didn't ultimately want to use going forwards.
For example the domains are www.some-thing.com and www.something.com.
They have asked if I can put a 301 permanent redirect in on the URL with the hyphen so then users should always go to the non hyphen version of the site.
I have tried URL rewrite rules and using the HTTP redirect in IIS however the site is then in a re-direct loop and I think this is down to the fact I have both domains bound to the site in IIS.
Help !!
Thanks in advance
Try this URL Rewrite .Here is the Source
<rule name="fromadd Host Name" stopProcessing="false">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^anitkb\.com$" />
</conditions>
<action type="Redirect" url="To address" redirectType="Permanent" />
</rule>
I am building an ASP.NET CMS-driven web application which will serve multiple websites under different domain names. Some of these will use www sub-domain, others will use custom sub-domains. There will be a variety of top-level country domains.
I'm looking for a generic IIS URL Rewrite rule that will redirect any URL which doesn't specify a sub-domain to its www equivalent.
When I say generic it means the rule cannot hard-code either domain name or top-level country domain. So the rule must redirect
http://anything.anywhere/any-path to http://www.anything.anywhere/any-path but leave http://sub.anything.anywhere/any-path.
The closest I've found is this which still hard-codes TLCD. Without much knowledge of the syntax of URL Rewrite I'm not sure how to handle any TLCD.
Thanks in advance.
Update:
Inspired by comment, I've had a go with regex, but haven't yet found a method that doesn't require me to hard-code a list of all possible TLCDs. I suspect this is the best I'll get. Can anyone refine or confirm this as the answer?
^([a-z]+[.](com|co.uk|de|fr|etc)+)*
I just did the exact same thing using a rewrite rule with two conditions, one to get the Scheme and one to determine if the www is missing. The scheme is necessary as the redirect has to be absolute, but if your not catering for HTTPS that could be hard-coded. Just bear in mind I've not had time to test the HTTPS part yet, but pretty sure it will work ok.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Root Redirect" stopProcessing="true">
<match url=".*" negate="false" />
<action type="Redirect" url="{C:1}://www.{HTTP_HOST}/{R:0}" />
<conditions trackAllCaptures="true">
<add input="{CACHE_URL}" pattern="^(.*)://" />
<add input="{HTTP_HOST}" pattern="^(?!www\.).*" />
</conditions>
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
I'm trying to use URL rewrite with IIS7 to make mydomain.com/home/default.aspx look like mydomain.com. Additionally, the original structure of the site has the root default.aspx redirecting to mydomain.com/home/default.aspx.
Thus, visits to mydomain.com would redirect to mydomain.com/home/default.aspx while showing only mydomain.com in the web browser address bar.
Can someone help me with the Inbound and Outbound rules to make this happen?
Thank you.
Run the iis manager (inetmgr), select your website -> double click on URL rewrite.
You then have to click on the right on Add Rule(s)... and choose Blank Rule.
Fill up the fields with the following values:
This rule will match mydomain.com or mydomain.com/ and rewrite it to mydomain.com/home/default.aspx.
This will produce the following rule in your web.config:
<rules>
<rule name="test" stopProcessing="true">
<match url="^/?$" negate="false" />
<action type="Rewrite" url="home/default.aspx" />
</rule>
</rules>
I'm currently planning to deploy a site with a third party hosting provider. I will only have access to the server via ftp and a tool similar to cpanel called WebsitePanel.
No access to IIS set up or configs.
Is there anyway to redirect http://www.example.com to http://example.com?
Place this in your web.config using your values for domain.com. This leverages the URL rewrite rules of the web.config and IIS 7.
<system.webServer> / <rewrite> / <rules>
<rule name="Remove WWW prefix" >
<match url="(.*)" ignoreCase="true" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.domain\.com" />
</conditions>
<action type="Redirect" url="http://domain.com/{R:1}"
redirectType="Permanent" />
</rule>
Typically, the "tool similar to cpanel" should give you this option.
Failing that, you should be able to:
a) set a custom 404 page pointing, to, say, myredirector.asp [or whatever server-side script you wish to use]
b) in myredirector.asp [or whatever] , do a server-side redirect as appropriate.
Not as clean as a straight IIS redirect, but it works pretty good.
I'd suggest you do this through the domain's DNS configuration, rather than through your application. It's much simpler and doesn't rely on application code to work (so if you deploy a whole new application, you don't have to remember to add any config entries or similar).
Same thing can be done to add the prefix www also. A blog post for the same at following URL:
http://karmic-development.blogspot.in/2013/10/add-prefix-www-automatically-in-url-in.html