301 redirects after domain name change - asp.net

I run a small ASP.NET4 website on IIS. I recently changed the domain name from 'olddomain.com' to 'newdomain.com'. The change was made through Plesk. After making the change I added 'olddomain.com' as a domain alias in plesk so that traffic would still route through to the website on the old domain.
This is fine so far. The website resolves on both old and new URLs.
However, now I would like to set up 301 redirects everywhere so that any request for olddomain.com are forwarded to newdomain.com instead. None of the website pages have changed - it's just a simple change of domain name.
I've added this to the web.config file:
<rewrite>
<rules>
<rule name="Redirect all to different domain" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^olddomain.com$" />
</conditions>
<action type="Redirect" url="http://www.newdomain.com/{R:0}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
But it doesn't seem to have any impact; when I load olddomain.com in the browser it just loads the website without redirecting to newdomain.com.
Can anyone suggest what I've done wrong here and how to get the 301 to work?
Thanks

This works:
<rule name="redirectDomain" stopProcessing="true">
<match url="(.*)" />
<action type="Redirect" url="http://www.newdomain.com/{R:1}" redirectType="Permanent" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^(www.)?olddomain\.com$" />
</conditions>
</rule>

You can set 301 permanent redirect from IIS also.
If http redirection is enabled on old server then you have to put new web config with the contents -
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<httpRedirect enabled="true" destination="http://mynewsite.com/" httpResponseStatus="Permanent" />
</system.webServer>
</configuration>
Let me know if it helps.

Related

Redirect non WWW site to https:// IIS

We have a non WWW website that we want to redirect to https://
The site is being hosted on IIS and we were looking to just type websitename.domain.com and have it redirect to https:// We have a URL rewrite rule in place but it when typing in the websitename.domain.com, it won't even try to go to https:// or http:// Is there something else we have to set for this to work?
Thanks.
First, make sure you bind the site bindings with websitename.domain.com for HTTP and https protocol like below:
Try below rule to redirect http to https:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="HTTPS force" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

Azure redirect non-www to www

I have question about Azure redirection.
I have searched other topics but not quite my problem(s).
I have ASP.NET Core 2.2 project and Azure configured for that. Also i have domain for pointing to Azure.
I have added www.example.com format domain to azure and that works fine also with https.
Problem rises with example.com format
I wanted to redirect example.com format to www.example.com URL
I manage to get this work with this, but for little while.
I use this web.config for redirection
web.config
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
<rewrite>
<rules>
<rule name="CanonicalHostNameRule" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^example.com$" />
</conditions>
<action type="Redirect" url="https://www.example.com/{R:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Something strange is Google Chrome & Opera redirect(s) to www domain but not Edge and Firefox.
Then when i clear cache(s) then it wont redirect.
Seems like Azure like rewrites some over or something.
Thanks for help!!
In addition to the web.config file changes, ensure the Azure Website instance contains the correct domain bindings within the Hostname bindings area.
<rule name="Redirect to www">
<match url=".*" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^example.com$" negate="true" />
</conditions>
<action type="Redirect" url="http://www.example.com/{R:0}" redirectType="Permanent"/>
</rule>

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.

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