Azure Web Sites custom domain - dns

I configured a custom domain for my Azure Web Site using CNAME records.
Everything works fine, except for the fact that I can access my site using both *.azurewebsites.net and *.com.
Isn't this a SEO issue, and can it be avoided so that I get *.com in both cases?

You can try with simple url rewrite rule in your web.config file:
<system.webServer>
<rewrite>
<rules>
<rule name="MyHostNameRule">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^domain\.com$" negate="true" />
</conditions>
<action type="Redirect" url="http://domain.com/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
Basically, it says that if request host doesn't match specified pattern, just rewrite it to domain.com/... If you have access to the IIS, you can use Url Rewrite module there, where this code can be created through a wizard.
One hint: when developing app and testing on localhost:port, it will rewrite url. To avoid that, put this rewrite rule in Web.Release.Config, and of course deploy your app to Azure WebSite in Release mode! Only thing you need is add xdt:Transform to rewrite element:
<rewrite xdt:Transform="Insert">

Related

Can an Azure App Service redirect www to non-www?

Is it possible to redirect www to non-www at the azure app service level?
I need this at the app service level and not in the configuration file of my app. I have been able to get http to https working under the tls/ssl blade, but I don't see a way to redirect for www.
Do I need another resource like front door or application manager? I'd like to handle it in the app service itself.
My app server is running a docker container that is using an nginx server. I'm not an expert with nginx, and haven't been able to get the redirect to work in the nginx configuration. Plus, I'd like to set this up in my bicep file.
To rewrite a URL, you need to create a web.config file in your wwwroot folder and put the necessary entries in there, if you don't already have a web.config file for your site.
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Redirect requests to default azure websites domain" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAny">
<add input="{HTTP_HOST}" pattern="^yoursite\.azurewebsites\.net$" />
</conditions>
<action type="Redirect" url="http://yoursite.com/{R:0}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Original answer: https://learn.microsoft.com/en-us/answers/questions/193377/how-to-redirect-in-app-service

web.config https redirect for multi-domain hosting

I've got a GoDaddy multi website hosting account and now that I've implemented SSL I want all those domains that have SSL to now redirect to https. GoDaddy says the web.config needs to be modified but they have no examples on how to do this for multiple domains.
I've tried the following code which works for the main domain (domainA) on the hosting account but then it messes up all the URLs for all the other domains hosted on that account. For example http://domainA.com redirects to https://domainA.com but with this code implemented http://domainB.com redirects to https://domainB.com/domainB/ -- domainB being the sub-folder that the other domain files are stored.
<configuration>
<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>
</configuration>
Try https://{HTTP_HOST}{REQUEST_URI} in your action.
Also I'd use temporary not permenant redirects while testing. Your browser may cache a bad redirect which can be very frustrating so make sure to clear your cache or use an in-private browser window to test changes.

Azure WebApp Subdomain Rewrite

I have what should be a simple rewrite URL, that takes our subdomain and rewrites it to a path on our main website. This works fine locally in IIS. However, it does not work within our webapp, any suggestions on how to point a subdomain to a folder in Azure? All the custom domains and certs are wired up correctly in Azure, and we have several other rules that are working, just not this one for the subdomain rewrite.
<rule>
<match url="*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_URI}" pattern="*://oursubdomain.*/*" />
</conditions>
<action type="Redirect" url="{C:1}://ourwebsite.com/oursubdomain/{C:3}" redirectType="Permanent" />
</rule>
Thanks for the help
It appears you have to be explicit when rewriting sub domains inside of Azure. As
Azure is already using wildcard to map the sub doamain. Replacing pattern="://oursubdomain.*/" with pattern="^oursubdomain.ourdomain.com$" the rule now works correctly.

Do I need anything besides code written in web.config to redirect website hosted on azure to https

I have a website that I host on azure. I recently bought an SSL and configured it. Now users can visit my site by typing in either http://example.com or https://example.com.
What I want is for users who type in the former to be automatically redirected to the latter while also keeping anything after the .com
So if a user types in http://example.com/about they will be redirected instead to https://example.com/about.
After some reading, I've come across this code that seems to do what I want
<system.webServer>
<rewrite>
<rules>
<rule name=”Redirect to https”>
<match url=”(.*)”/>
<conditions>
<add input=”{HTTPS}” pattern=”Off”/>
<add input=”{REQUEST_METHOD}” pattern=”^get$|^head$” />
</conditions>
<action type=”Redirect” url=”https://{HTTP_HOST}/{R:1}”/>
</rule>
</rules>
</rewrite>
</system.webServer>
But before I add this to my web.config file I have a few questions.
What is the IIS url rewrite module? IIS Rewrite and is it required to be installed on my azure hosted websites before I upload my new web.config file.
How can I also include removing www from my URL when a user enters it. For example if a user types in www.example.com they should be redirected to https://example.com instead. The reason that I want this is because in my google search console I've told google to display URLs as example.com rather then www.example.com
and finally, will this code do what I'm looking for? Is there a more professional way to achieve this? What are the benefits. I should note that my sites are asp .net web forms. I know MVC has routing options but that is not an option for me.
Edit : I don't think How to force HTTPS using a web.config file solves my issue because I don't even know if I can install the URL Rewrite module since I am not hosting IIS myself. Does azure give you access to the IIS settings? I am unfamiliar with azure details.
The Microsoft URL Rewrite Module for IIS enables IIS administrators to create powerful customized rules to map request URLs to friendly URLs that are easier for users to remember and easier for search engines to find.
This module is pre-installed for Azure Web App, as shown when inspect the applicationHost.config of the Azure Web App in Kudu.
Hence, you do not need to worry about the availability of the module for Azure Web App.
The URL Rewrite configuration to enforce HTTPS redirection for Azure web app is the simplest way to achieve what you intend. Your above configuration will apply
only if the request method is either HTTP GET or HTTP HEAD. The below configuration will not have such limitation.
<system.webServer>
<rewrite>
<rules>
<rule name="Force HTTPS Redirection" enabled="true" stopProcessing="true">
<match url="^$" ignoreCase="false"/>
<conditions>
<add input="{HTTPS}" pattern="^OFF$"/>
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/" redirectType="Permanent"/>
</rule>
</rules>
</rewrite>
</system.webServer>
I would add one last thing. Assuming you are running on Azure Web Apps, they have various probes to your site for warm up and initialization. You probably don't want these probes to also be redirected, otherwise, you may have some issues when you restart or use Azure's swaps feature for stuff like blue/green deployments. These probes would then be return with a 301/302 rather than actually hitting your site (and Azure doesn't actually follow the redirect)
More examples https://github.com/projectkudu/kudu/wiki/Xdt-transform-samples
<rule name="Redirect to non-WWW" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="www.example.com$" />
<add input="{HTTP_USER_AGENT}" pattern="Initialization" negate="true" /> <!-- IIS Application Initialization Warmup -->
<add input="{HTTP_USER_AGENT}" pattern="SiteWarmup" negate="true" /> <!-- Azure WebApps Warmup Request -->
<add input="{HTTP_USER_AGENT}" pattern="AlwaysOn" negate="true" /> <!-- Azure WebApps AlwaysOn Probes -->
</conditions>
<action type="Redirect" redirectType="Permanent" url="https://example.com/{R:1}" />
</rule>
<!-- Redirect to HTTPS Version -->
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
<add input="{HTTP_USER_AGENT}" pattern="Initialization" negate="true" />
<add input="{HTTP_USER_AGENT}" pattern="SiteWarmup" negate="true" />
<add input="{HTTP_USER_AGENT}" pattern="AlwaysOn" negate="true" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="https://{HTTP_HOST}/{R:1}" />
</rule>

IIS URL Redirect failing

I need to redirect URLs of the form:
http://server1.name.here/path1/path2/?num=123456
to:
http://server2.name.here/path3/path2/?num=123456
using the IIS URL Rewrite Module 2.0 on IIS 8.5. I've used a DNS alias to handle the server name redirection.
I've been using the user interface to configure the path rewrite but despite trying several variations and extensive research I cannot get my rewrite to work, which is slightly embarrassing as I feel this should be a simple rewrite. The web.config produced by the URL rewite user interface is:
<rewrite>
<rules>
<rule name="path rewrite" patternSyntax="ECMAScript" stopProcessing="false">
<match url="path1/path2/" ignoreCase="true" />
<conditions>
<add input="{QUERY_STRING}" pattern="num=[0-9]+" />
</conditions>
<action type="Rewrite" url="path3/path2/" appendQueryString="true" logRewrittenUrl="true" />
</rule>
</rules>
</rewrite>
When I try to open server1.name.here/path1/path2/?num=123456 I get a "403 - Forbidden: Access is denied." and the logs show no evidence that my URL is being rewritten or even flagged by the rewrite rule.
I'm sure I'm missing something obvious, can anyone enlighten me as to what I've got wrong?
Many Thanks Eden

Resources