How to share cookie between domain and subdomain, but not other subdomains - asp.net

Our ASP.NET MVC web application has a few different subdomains we use for testing and legacy code. The subdomains are:
www.sitename.com (production site)
test.sitename.com (testing)
original.sitename.com (legacy code)
staging.sitename.com (occasionally used to testing right before a deployment)
We purposefully have the forms authentication not using domain level cookies because we want the cookies to be unique across these different subdomains. The problem is, when people get a link to the root domain (sitename.com), it requires them to log in again to get a cookie, even though they're already logged in to www.sitename.com.
Is there a way to share the cookie between only www.sitename.com and sitename.com without the other subdomains being affected?

You can avoid this problem by redirecting your non www domain to www with UrlRewrite module in >IIS7
rewrite rule to put into web.config
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to WWW" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^example.com$" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:0}"
redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>

I'd recommend forcing the use of the www. version of the site, for this reason amongst others, this site has excellent reasons why...
http://www.yes-www.org/why-use-www/
To do this in .net you can add the following to your web.config
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to www" stopProcessing="true">
<match url="(.*)" />
<conditions trackAllCaptures="false">
<add input="{HTTP_HOST}" pattern="^sitename.com$" />
</conditions>
<action type="Redirect" url="{MapProtocol:{HTTPS}}://www.{HTTP_HOST}{HTTP_URL}" redirectType="Permanent"/>
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="MapProtocol">
<add key="on" value="https" />
<add key="off" value="http" />
</rewriteMap>
</rewriteMaps>
</rewrite>
</system.webServer>
This will auto-redirect permanently (see the addition of redirectType="Permanent") for non-www URLs to the www equivalent and retain the HTTP(s) protocol.
The trackAllCaptures part is related to the regex pattern matching - in our case we do not need to capture anything; we only need to match for the rule, so we can leave as false.
The regex pattern ^sitename.com$ will match when the hostname matches exactly to "sitename.com" - the ^ means the start position and the $ means the end position
The rewrite map is from an idea from Jeff Graves I believe, http://jeffgraves.me/2012/11/06/maintain-protocol-in-url-rewrite-rules/
The way I have shown shows just one way to do this, like with most things - there are multiple ways on achieving this.
Scott Forsyth has an article on a different way of achieving this too (also references Jeff Graves)
http://weblogs.asp.net/owscott/url-rewrite-protocol-http-https-in-the-action

You can use some thing like
sessionCookie.Domain = ".yourdomain.com" ;
then you will be able to request same cookies from any subdomain and edit it if you want.

Related

IIS 7.0 URL REWRITE

I am setting up a redirect to WWW for one of our sites in the web.config and ran into a small issue. The code I have in the web.config for the rewrite is as follows :
<rewrite>
<rules>
<rule name="Redirect to www" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" />
<conditions>
<add input="{HTTP_HOST}" pattern="example.com" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:0}" />
</rule>
</rules>
</rewrite>
I'm finding that it's actually working a little too well. Because of the pattern "example.com", I'm seeing that it's now redirecting to our live site on dev and staging because our URLS are laid out like so : dev.example.com & staging.example.com. For the time being, I have just commented out the rewrite on these other web.configs but I'm wondering if there's a better pattern or option to get around this issue.
If you only want the root domain without subdomains then you should edit your pattern in the HTTP_POST section.
Place a ^ in front of the pattern which means start with. So If the url starts with example.com then it gets redirected to www.example.com.
If its dev.example.com, this rule will be ignored.
Edit your example:
<add input="{HTTP_HOST}" pattern="^example.com" />

iis redirect subdomain to subfolder on same subdomain

I have an IIS site and Im trying to use ReWrite 2.0 to redirect a particular subdomain to a sub folder. Within my IIS site I have it binded to two different domains:
one.example.com
two.example.com
When people visit one.example.com I want it to do nothing. When people visit http://two.example.com I want them to be redirected to http://two.example.com/subfolder.
Thanks for your help.
You need to add a match condition for the HTTP_HOST
<system.webServer>
<rewrite>
<rules>
<rule name="two.example.com Redirect" stopProcessing="false">
<match url="^\/?$" />
<conditions>
<add input="{HTTP_HOST}" pattern=".*two\.example\.com.*" />
</conditions>
<action type="Redirect" redirectType="Found" url="http://two.example.com/subfolder/{R:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>
Here's a decent overall reference for the module:
http://www.iis.net/learn/extensions/url-rewrite-module/url-rewrite-module-configuration-reference
A very late answer, but hopefully it helps someone.
I presume you are using II7 or higher ?
IIS has a simple HTTP redirect function which is available per site listed in IIS.
Make sure you are in features view. Click on the site you want to redirect (In your case its http://two.example.com)
You should see this. Double click on HTTP Redirect
The you should see this. Enter your Redirect URL here (In your case its http://two.example.com/subfolder)
This existing question should solve your issue
<system.webServer>
<rewrite>
<rules>
<rule name="Root Hit Redirect" stopProcessing="true">
<match url="^$" />
<action type="Redirect" url="/menu_1/MainScreen.aspx" />
</rule>
</rules>
</rewrite>
</system.webServer>

a web application in the cloud as a subfolder of the main company URL

Is it possible to deploy a web application in the cloud and configure it so that it appears like a subfolder in the company URL.
E.g.
www.mycompany.com //main company web site
www.mycompany.com/products // hits a web application in the cloud (azure for example)
thanks
Having in mind it is easier to do with sub-domain and CNAME, I think that what you are looking for is the IIS URL Rewrite module.
The following base rules are taken from this IIS Forum's thread, as a similar situation is described and samples rules are given. You may use them as a base:
<system.webServer>
<rewrite>
<rules>
<rule name="CanonicalHostNameRule">
<match url="(.+)/(.*)" />
<conditions>
<add input="{HostToRedirect:{HTTP_HOST}/{R:1}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="http://{C:1}/{R:2}" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="HostToRedirect">
<add key="domain.com/mail" value="mail.domain.com" />
<add key="domain.net/mail" value="mail.domain.net" />
</rewriteMap>
</rewriteMaps>
</rewrite>
</system.webServer>
You may also check this SO question, as it is providing some guides on the problem you are trying to solve.
UPDATE WITH SOLUTION
This is totally achievable with URL Rewrite module for IIS in combination with ARR module. Then you need to set a Reverse Proxy rule that will rewrite all /products requests to the corresponding web app in the cloud. Your rule will look something like that:
<system.webServer>
<rewrite>
<rules>
<rule name="ReverseProxyInboundRule1" patternSyntax="ECMAScript" stopProcessing="true">
<match url="(^products)" />
<conditions>
<add input="{CACHE_URL}" pattern="^(https?)://" />
</conditions>
<action type="Rewrite" url="{C:1}://myvompany.cloudapp.net/" />
</rule>
</rules>
</rewrite>
</system.webServer>
You can tweak this later to fit your exact need, but with the above sample I just achieved what you are looking for.
I'm not a DNS expert, but I think you would find it more straightforward to route products.mycompany.com to your Windows Azure web site than a subfolder.
IIS support redirects
I also like the answer from David on making it a web site and use cname to map products.mycompany.com
You could do both. Redirect www.mycompany.com/products to products.mycompany.com

Forcing HTTPS and avoid duplicate URLS using IIS7 URL Rewrite Module

I need to force every request to https://www.mysite.com (always with https and www)
The site is hosted in GoDaddy and I need to do it via IIS7 URL Rewrite Module.
I've been able to do the HTTPS redirect with the following code:
<system.webServer>
<rewrite>
<rules>
<rule name="Canonical Host Name" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^mysite\.com$" />
</conditions>
<action type="Redirect" url="https://www.mysite.com/{R:1}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
Test cases
http://mysite.com -> https://www.mysite.com OK
http://www.mysite.com -> https://www.mysite.com NOT WORKING
I guess the condition is not being satisfied when I enter www.mysite.com in the browser, so there's no redirect and the page serves as HTTP instead of HTTPS.
I think I just need to modify the condition pattern, but I have almost nothing regex knowledge and I need this asap.
Thanks!
emzero, I think the issue is that your condition only matches precisely mysite.com:
<conditions>
<add input="{HTTP_HOST}" pattern="^mysite\.com$" />
</conditions>
Note the pattern: ^mysite\.com$. This says, in English, that the incoming URL must start with mysite.com and end with mysite.com, meaning www.mysite.com will not be matched.
Try this pattern instead, which allows for an option www.:
<conditions>
<add input="{HTTP_HOST}" pattern="^(www\.)?mysite\.com$" />
</conditions>
Happy Programming!

ASP.NET / IIS7 Url Rewrite maps not working

I've followed the instructions Learn IIS's webpage for adding static redirects with a rewrite map for my asp.net application.
The following is the config:
<rule name="Redirect rule1 for Information" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false">
<add input="{Information:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Rewrite" url="{C:1}" appendQueryString="true" />
</rule>
And
<rewriteMaps>
<rewriteMap name="Information">
<add key="/Information/CorporateSales.aspx"
value="/KB/Information/CorporateSales" />
<add key="/Information/ComputerRepair.aspx"
value="/KB/Information/ComputerRepair" />
</rewriteMap>
</rewriteMaps>
This was even originally created by the wizard in IIS's manager for using rewrite maps.
So the idea is that /Information/CorporateSales.aspx --> /KB/Information/CorporateSales with a 301 redirect (MOVED PERMANENTLY).
However I'm just getting the original aspx page (Which we're removing later) loading. I've even deleted the file incase it was defaulting to an existing resource, and with that i just get a plain 404 without the redirect.
Anyone have an idea?
Let me clarify something:
Rewrite module works, it's installed and running. My standard regex rules work nicely. But my rewrite map does not.
This article http://www.iis.net/learn/extensions/url-rewrite-module/using-rewrite-maps-in-url-rewrite-module and code below worked for me.
<rewrite>
<rules>
<rule name="Redirect rule1 for RedirectURLs">
<match url=".*" />
<conditions>
<add input="{RedirectURLs:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="false" />
</rule>
</rules>
<rewriteMaps>
<rewriteMap name="RedirectURLs">
<add key="/privacy.php" value="/privacy" />
</rewriteMap>
</rewriteMaps>
</rewrite>
I was having a similar problem and found this question. It took me a little while, but I was able to figure out what the problem was.
My rewriteMap contained the urls "/Default2.aspx" and "/Dashboard.aspx".
When I would go to Default2.aspx, I would get a 404 rather than get redirected to Dashboard.aspx as expected.
The issue I found was that on my machine, the application was running in a subdirectory. The rewriteMap paths would only work if I used the full path (including the application folder), e.g., "/TestSite/Default2.aspx".
So I could have added duplicate entries in my rewriteMap to account for application directories on developer machines, but that seemed messy. I looked at the other rewrite rules in the application that did not have this issue and I noticed that they were using the {REQUEST_FILENAME} variable, rather than {REQUEST_URI}. So I switched the rule to use {REQUEST_FILENAME} and remove the first slash from the urls in my rewriteMap.
Do you have Url rewriting installed as part of IIS7/7.5? This is not installed by default. Also, make sure your app pool is set to integrated pipline mode, no classic.
Edit
From this:
http://learn.iis.net/page.aspx/469/using-rewrite-maps-in-url-rewrite-module/
This only thing I see that you're doing is adding the 'stopProcessing' attribute. Have you tried removing that?
Previously I had same problem as you described.
Could you update your code to
<match url="(.*)" />
and I hope you aware,
<add input="{Information:{REQUEST_URI}}" pattern="(.+)" />
this condition will capture full URL except the domain.
example on this url:
www.example.com/Information/CorporateSales.aspx
it will check matching condition of
Information/CorporateSales.aspx on rewriteMap
and for sure it wont be match with this url
www.example.com/old/Information/CorporateSales.aspx
Did you reset the app pool and the iis site ?
In some cases it can take up to 72 hours (iirc) to propagate throughout the world.

Resources