how to set asp.net default subdomain to www? - asp.net

If have a website myws.com, if I request myws.com or www.myws.com I will see the same website
what I want is to be redirected to 'www.myws.com' when requesting 'myws.com'
Thanks

There are many ways to achieve a canonical redirect for the www subdomain, if you're using ASP.NET 4.0 you can do this:
if (!Request.Url.Host.ToUpper().Contains("WWW"))
{
Response.RedirectPermanent("http://www.myws.com/" + Request.Url.PathAndQuery, true);
}
You can also use the IIS URL Rewrite module, once you've installed it add this to your web.config file:
<rewrite>
<rules>
<rule name="WWW Canonical Redirect" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^myws.com$" />
</conditions>
<action type="Redirect" url="http://www.myws.com/{R:0}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>

If you've got access to IIS, you can use URL rewrite to permanently redirect traffic from myws.com to www.myws.com. Follow this tutorial: http://weblogs.asp.net/owscott/archive/2009/11/27/iis-url-rewrite-rewriting-non-www-to-www.aspx for URL rewrite.
If you don't have access to IIS, then you can add the following code into your global.asax.vb within the Application_BeginRequest event:
If Request.Url.Authority.ToString <> "www.myws.com" Then
Response.RedirectPermanent(Request.Url.Scheme.ToString() & "://www.myws.com" & Request.Url.AbsolutePath.ToString())
End If

Related

HTTP to HTTPS Redirect in IIS

I have to redirect my site from http to https, whenever any user open my site using http.
For example: -
http:\\abc.mywebsite.com should go to https:\\abc.mywebsite.com
Notice that in above URL, it is not www.mywebsite.com, instead it is custom URL as abc.mywebsite.com.
I have tried URL Rewrite tool and followed all steps mentioned here. However, I cannot get URL Rewrite to work properly to redirect.
Here is the URL Redirect rule looks like in IIS: -
Here is how my web.config looks like after adding rule using URL Rewrite.
<configuration>
.....
.....
.....
<system.webServer>
.....
.....
.....
<rewrite>
<rules>
<rule name="Http to Https" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
.....
.....
.....
</configuration>
I have also uncheck Require SSL check box under SSL Settings.
However, after doing all this, my website is still not redirecting to https. Just giving error "...can't reach this page".
Please suggest if I am missing anything here.
I have some doubts about the rewrite rules because I see many examples that are different.
Trying different values like the one from this Microsoft Blog could yield better results:
<rewrite>
<rules>
<rule name="Redirect to http" enabled="true" patternSyntax="Wildcard" stopProcessing="true">
<match url="*" negate="false" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Found" />
</rule>
</rules>
</rewrite>
You can also setup Tracing for Failed Requests in IIS 7. This could give you insight into why it's failing to redirect.
I was facing same issue within the same site when trying to redirect from http to https.
I did created new site and added the binding as http://www.example.com/
And then I have created new Http Redirect and added my https://www.example.com/ it worked.
Make sure that you have setup the bindings correctly in IIS, the second line highlighted in red should be there.
Type = HTTPS
Host Name = the site address like "site.domain.com"
Port = 443
IP Address = The IP Address of the server you are trying to access.

Web.config rewrite http + www and http non www and https non-www to to https://www

I have recently switched my asp.net MVC website to https and want all existing traffic to get redirected to https://www.example.com using web.config rewrite.
I have tried various combinations but not yet successful.
need to handle following three scenarios :
http://
http://www
https://
Try this it might help you:
<system.webServer>
<rewrite>
<rules>
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" redirectType="Permanent"
url="https://{HTTP_HOST}/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
You can redirect your traffic to use the https using the Global.asax as well.
Method name: Application_BeginRequest
Your code to redirect will look something like this
strProtocol = "https";
if (HttpContext.Current.Request.Url.ToString().ToLower().StartsWith("http:") == true)
{
Response.Redirect(strProtocol + "://" + strYourHostName + strYourRemainingURL, false);
}
you can keep the protocol and hostname in the web.config and pick from there.

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>

ASP.net MVC site : Redirect all "non WWW" request to WWW

Recently I migrated an ASP.net site to ASP.net MVC site. Earlier there were two host headers one mydomain.com and another is www.mydomain.com. My SEO says you should use only one url "www.domain.com" for SEO advantage.
I am looking for an option to do 301 permanent redirect all mydomain.com request to www.mydomain.com.
The site is hosted in IIS6 and developed in ASP.net MVC 4.
You can do this from your web.config file
<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>
You could use config or Url Rewriter in IIS, but the best method I've found is just to put some code in Application_BeginRequest() in your global.asax.cs like this:
var HOST = "www.mydomain.com";
if ( !Request.ServerVariables[ "HTTP_HOST" ].Equals(
HOST,
StringComparison.InvariantCultureIgnoreCase )
)
{
Response.RedirectPermanent(
( HttpContext.Current.Request.IsSecureConnection ? "https://" : "http://" )
+ HOST
+ HttpContext.Current.Request.RawUrl );
}
Because you're doing this in code, you can have whatever logic you need on a per-request basis.
Unfortunately, the URL rewrite module does not work with IIS6 (only IIS7 or greater). Have you considered creating your own HttpModule, something like this this?
IIS 6 how to redirect from http://example.com/* to http://www.example.com/*
Or you could potentially use a 3rd party solution like one of these:
http://iirf.codeplex.com/
http://www.urlrewriting.net/149/en/home.html
http://www.isapirewrite.com/
http://urlrewriter.net/
(IIS 7 or greater required)
from http://www.codeproject.com/Articles/87759/Redirecting-to-WWW-on-ASP-NET-and-IIS
(Similar to above solution, but not requiring you to add your own domain name.)
<configuration>
<system.webServer>
<rewrite>
<rules>
<clear />
<rule name="WWW Rewrite" enabled="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true"
pattern="^www\.([.a-zA-Z0-9]+)$" />
</conditions>
<action type="Redirect" url="http://www.{HTTP_HOST}/{R:0}"
appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Note that you will most likely see squiggly lines under the tag with a message that the tag is invalid. I got this message but, in fact, it worked just fine.
If you want the intellisense to work, you can try this update here...
http://ruslany.net/2009/08/visual-studio-xml-intellisense-for-url-rewrite-1-1/
More information about httpRedirect can be found here...
http://www.iis.net/configreference/system.webserver/httpredirect

Configure redirect asp.net/iis7

I have a website that opens when the user enters either http://example.com or http://www.example.com.
I need to configure the server or the app to redirect with a permanent redirect to www.example.com when example.com is accessed.
What is very important is that the path is preserved, so if example.com/path/page.aspx?p=1, the redirect should be done to www.example.com/path/page.aspx?p=1.
thanks!
Using the URL Rewrite you can do this by adding a configuration in you web.config. You need to install this module in your IIS as well. Here is an example, not fully tested:
<system.webserver>
<rewrite>
<rules>
<rule name="Redirecting" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP}" pattern="^(http://)?example.com" ignoreCase="true" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="http://www.example.com/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webserver>
The URL Rewrite module should do exactly what you need.

Resources