简体   繁体   中英

Redirecting non-WWW to WWW in an Azure Website

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?

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>

If you want a REAL redirection (ie when a user types example.com then the address in the browser automatically changes to www.example.com ) then you have two options:

  1. Using the forwarding feature offered by GoDaddy (you can find it in the GoDaddy dashboard (domain details page). In this way you can point example.com to a GoDaddy IP that responds with a redirection to www.example.com
  2. Write some code in ASP.NET that detects when the address is missing "www." and then redirecting to 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:

  1. Get the virtual IP address associated to your Azure website: from the Azure management portal click on your website, go to the dashboard section and click Manage Domains. You should get something like "The IP address to use when you configure A records: xxx.xxx.xxx.xxx".
  2. Go to GoDaddy and set an A record with "Host" to @ and "Points to" set to the IP found at step 1
  3. Add a CNAME record with "Host" set to awverify and "Points to" set to the address of your azure website prefixed with awverify (for example awverify.mywebsite.azurewebsites.net )
  4. Add a CNAME record with "Host" set to www and "Points to" set to the address of your azure website (for example mywebsite.azurewebsites.net )
  5. Save the zone file in GoDaddy
  6. Go back to windows azure in the "Manage Domains" section of your website and add both 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/

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>

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()
            );

Would this just be a temporary 302 redirect ? A perm 301 redirect using rules in the web.config would be better for SEO.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM