Reputation: 561
I have a domain with Godaddy, and a website on the Azure websites infrastructure. What I want to achieve is to use only the www version of my domain. If a user enters "example.com" in their browser I want them to be redirected to "www.example.com".
The site is hosting a ASP.Net MVC 5 app if that makes a difference. How do I configure this?
Upvotes: 26
Views: 23323
Reputation: 20071
In ASP.NET Core MVC, add this to the Startup.cs
Configure()
method.
app.UseRewriter(new RewriteOptions()
// redirect non www to www.
.AddRedirectToWwwPermanent()
// While we are at it, let's also redirect http to https.
.AddRedirectToHttpsPermanent()
);
Upvotes: 8
Reputation: 4876
Instead of a generic MatchAll with negate=true, I prefer to catch only the naked domain and redirect if it is a match. This works well for Azure since I am only interested in a single domain and then I don't have to write a bunch of exclusions for localhost, localtest.me, subdomains, etc.
Here is the rule... just change example
in the pattern to your domain:
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect To WWW" enabled="true" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^example\.com$" />
</conditions>
<action type="Redirect" url="http://www.{HTTP_HOST}{URL}" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
Upvotes: 4
Reputation: 836
If you want a REAL redirection (i.e. when a user types example.com
then the address in the browser automatically changes to www.example.com
) then you have two options:
example.com
to a GoDaddy IP that responds with a redirection to www.example.com
www.example.com
However, if you just want the users that type example.com
to get the same content as users typing www.example.com
and you don't mind people seeing example.com
without www in their address bar, then proceed as following:
@
and "Points to" set to the IP found at step 1awverify
and "Points to" set to the address of your azure website prefixed with awverify
(for example awverify.mywebsite.azurewebsites.net
)www
and "Points to" set to the address of your azure website (for example mywebsite.azurewebsites.net
)example.com
and www.example.com
to the list of domain names. If you get any error at step 6, just wait some hours to let the DNS changes to propagate and retry.
More info here: https://www.windowsazure.com/en-us/documentation/articles/web-sites-custom-domain-name/
Upvotes: 21
Reputation: 1514
add this code this code under <system.webServer>
section
<rewrite>
<rules>
<rule name="Redirect to www">
<match url=".*" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" negate="true" />
</conditions>
<action type="Redirect" url="http://www.{HTTP_HOST}/{R:0}" redirectType="Permanent"/>
</rule>
</rules>
</rewrite>
Upvotes: 25