IIS Host Headers and non WWW to WWW - iis-7

I know there's a bunch of examples on how to redirect your non www to your www site, but I'm not using any rewrite utils/ISAPI.
On my Windows 2008R2 box, I have several sites setup in IIS. I setup host headers for both www and non www versions. The first couple of sites work fine. If you try to go to the non www site, you are automatically redirected to the www version.
As far as I recall, I didn't have to do anything special other than add the appropriate host headers - no messing around with rewrites/ISAPI.
What am I missing on the server manager side of things in order to get this working?

I guess there are two ways. One is to create a rewrite rule through the IIS manager.
The other is to setup the system.webserver section of the web.config as follows:
<system.webServer>
<rewrite>
<rules>
<clear/>
<rule name="Redirect Non WWW to WWW" enabled="true" stopProcessing="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>
<!--<rule name="Default Document" stopProcessing="false">
<match url="(.*)default.aspx"/>
<action type="Redirect" url="{R:1}" redirectType="Permanent"/>
</rule>-->
</rules>
</rewrite>
<validation validateIntegratedModeConfiguration="false"/>
<modules runAllManagedModulesForAllRequests="true"/>
<httpErrors errorMode="Custom"/>
</system.webServer>

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>

301 redirects after domain name change

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.

ssl and rewrite tag does not recognized by visual studio in mvc3 web site web.config

I've written a simple web application using asp.net mvc3 and I have a login page which control access to the site. Every request which is not authenticated will redirect to this page.
I want to use ssl to make the site secure (using https). I don't want to use https for all of my sites because it make it heavy for browsers.
The problem is when I add following tags to web.config, VS2010 ultimate does not recognize it and does not work.
I have deployed the project to IIS 7.5 too but it does not work either. what should I do?
<system.webServer>
<rewrite>
<rule name="Redirect to HTTP">
<match url="secureDir/(.*)" negate="true" />
<conditions>
<add input="{HTTPS}" pattern="^ON$" />
</conditions>
<action type="Redirect" url="http://{HTTP_HOST}{REQUEST_URI}" />
</rule>
<rule name="Redirect to HTTPS" stopProcessing="true">
<match url="secureDir/(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" />
</rule>
</rewrite>
<validation validateIntegratedModeConfiguration="false"/>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
You'll need the URL Rewrite extension for IIS and the rewrite tag only works with IIS, so you'll need to be using that for your project. You don't neccesarily need VS to validate your xml if it's correct, but if you want it, there's a guide here: http://ruslany.net/2010/04/visual-studio-xml-intellisense-for-url-rewrite-2-0/

Redirect to www in IIS7 - classic pipeline mode

I want to implement redirects on an IIS7 webserver. Basically, if the subdomain is not included in the URL, I will redirect to the www subdomain.
http://mysite.com/file.aspx redirects to http://www.mysite.com/file.aspx
http://mysite.com/image.jpg redirects to http://www.mysite.com/image.jpg
http://mysite.com/text.html redirects to http://www.mysite.com/text.html
How to do this?
I do not want to write any HTTP Module -- it must be done thru IIS config only.
Also, I am required to use Classic Pipeline mode and cannot install any ISAPI plugins.
Is it possible?
You can throw this into your web.config file:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect to WWW" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^http://mysite.com$" />
</conditions>
<action type="Redirect" url="http://www.mysite.com/{R:0}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
In IIS7 it can be done through the url rewrite section.
This solution worked for me:
1) Install URL Rewrite component:
http://www.iis.net/download/urlrewrite
2) Add to web.config:
<system.webServer>
<rewrite>
<rules>
<rule name="CanonicalHostNameRule1" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^mysite\.com$" />
</conditions>
<action type="Redirect" url="http://www.mysite.com/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>

Resources