IIS10 UrlRewrite Canonical Rule with Multiple Domains - iis

I am using the DNN CMS that allows multiple portals (many websites) under 1 IIS website. In this situation I cannot use the standard canonical rule below because I have multiple bindings for different domains but am getting errors on Google search console because I don't have a canonical rule. How can I modify my rule to account for the multiple domains?
<rule name="CanonicalHostNameRule1" enabled="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HTTP_HOST}" pattern="^mydomain1\.com$" negate="true" />
<add input="{HTTP_HOST}" pattern="^(.*)\.mydomain1\.com$" negate="true" />
</conditions>
<action type="Redirect" url="http://mydomain1.com/{R:1}" />
</rule>
If in my bindings I had:
www.mydomain1.com
mydomain1.com
www.exampledomain.com
exampledomain.com
www.thirddomain.com
thirddomain.com
I would like to set the www. versions of all my domains as the canonical target but each domain should have it's own not 1 globally.
Is it just a matter of adding more input's to that rule? IE:
<add input="{HTTP_HOST}" pattern="^thirddomain\.com$" negate="true" />
----- ADDITIONAL INFO --------
In one rule I need requests to
mydomain1.com sent to www.mydomain1.com
exampledomain.com sent to www.example.com
thirddomain.com sent to www.thirddomain.com

Related

URL Rewrite IIS 7.5 - only for port 80

I setup IIS to redirect non https traffic to https by using the rewrite wizzard in IIS. I followed the steps outlined here: https://www.namecheap.com/support/knowledgebase/article.aspx/9953/38/iis-redirect-http-to-https.
Now I'm wanting to add a condition to say if the {server_port} = 80.
I added a new condition:
Condition Input: {Server_Port}
Check if input string: matches the pattern
pattern: 80
Under conditions : Logical Grouping: match all
I then saved the rule and restarted IIS.
I'm still getting all traffic to my site redirected to https which tosses Error code: SSL_ERROR_RX_RECORD_TOO_LONG since https:// www.mysite.com:83 does not compute.
How do I write the condition to redirect only if the server port is 80?
Please be aware that I have not written my redirect in the web.config and am asking because most of the examples I've found discuss web.config. I did find a web config example that should work, but I can't figure out how to use the rewrite tool to implement this:
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
<add input="{SERVER_PORT}" pattern="XXXX" negate="true" />
</conditions>
<action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" />
</rule>
I found simple mistake in your rule condition is <add input="{SERVER_PORT}" pattern="XXXX" negate="true" /> negate="true" means it does not match pattern and it rediect to the https. to impemnet your requiremnet you could use bwlow rule:
<rule name="HTTP to HTTPS redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" />
<add input="{SERVER_PORT}" pattern="80" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Found" />
</rule>

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>

Why are URL rewrites in Azure Web App virtual directory not being processed when root directory contains rewrites?

I have two virtual directories in an Azure Web App and confirmed that they're working properly as application directories.
The root of the Web App contains nothing except a web.config file with two rewrite rules that cause subdomains to point to subfolders like this:
<rule name="App1 Subdomain Redirect to SubFolder">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^app1.myservice\.net$" />
</conditions>
<action type="Rewrite" url="/app1/{R:0}" />
</rule>
<rule name="App2 Subdomain Redirect to SubFolder">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^app2.myservice\.net$" />
</conditions>
<action type="Rewrite" url="/app2/{R:0}" />
</rule>
(I understand this could be written more efficiently - I'm trying to remove as many variables as possible.)
In each of the virtual directories I have another web.config that contains a single rule to add an extension:
<rule name="Add Extension">
<match url=".*" negate="false" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{URL}" pattern="(.*)\.(.*)" negate="true" />
</conditions>
<action type="Rewrite" url="{R:0}.ashx" />
</rule>
The problem I'm experiencing is that the "Add Extension" rule is never processed.
If I remove the rule from the virtual directory's config file and put it into the root's, all is well. (If I only do this for one virtual directory, it works fine for that directory while the other does not. If I do it for both, then all rules get processed.)
If I remove the "Add Extension" rule and try other rules the other rules do not get processed, which indicates to me this is not a problem specific to this particular rule.
If I remove all rules from the root's config, all rules in the virtual directories get processed.
My question is, has anyone else experienced this and what have they done as a workaround?
It's my understanding that rules should cascade from root to virtual directories in the same way other configuration settings do.
Searches have not been helpful.
Anand seems to have experienced the same problem on a non-Azure IIS site but no answer was proposed: http://forums.iis.net/t/1178488.aspx
Zielu1's question is somewhat similar but again did not receive a response: IIS URL rewrite in child virtual directory not redirecting
There are a fair number of other questions about rewrites, mostly about syntax and getting them to work in general, but few about virtual directory inheritance of rules.
Thanks for the help.
Try it matching full subdirectory paths
<rule name="Add Extension">
<match url="/MySubDirectory/.*" negate="false" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{URL}" pattern="(.*)\.(.*)" negate="true" />
</conditions>
<action type="Rewrite" url="{R:0}.ashx" />
</rule>

IIS 7 URL Rewrite - CamelCase domain and lower case everything else

I am trying to setup rewrite rules for my site in iis 7 with the URL Rewrite module. If the site name is "WonderfulWidgets"
I want it to always be http://WonderfulWidgets.com.
NOT: wonderfulwidgets.com
NOT: WONDERFULWIDGETS.com
I also want everything after WonderfulWidgets.com to be lower case.
IE WonderfulWidgets.com/best-widgets.
I have accomplished the lower case url rewrite and I have also made it so it will remove any leading www before WonderfulWidgets.com
My problem is my lower case URL rewrite lowers the domain name too. I need help writing the CamelCase domain name that works with rewriting everything else as lower case.
Here's what I have in my web.config:
<rewrite>
<rules>
<rule name="CanonicalHostNameRule1">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^WonderfulWidgets\.com$" negate="true" />
</conditions>
<action type="Redirect" url="http://WonderfulWidgets.com/{R:1}" />
</rule>
<rule name="RemoveTrailingSlashRule1" stopProcessing="true">
<match url="(.*)/$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="{R:1}" />
</rule>
<rule name="Default Document URL Rewrite" stopProcessing="true">
<match url="(.*?)/?Default\.aspx$" />
<action type="Redirect" url="{R:1}/" />
<conditions>
<add input="{URL}" pattern="WebResource.axd" negate="true" />
</conditions>
</rule>
</rules>
</rewrite>
DNS names are generally treated as case insensitive, and so most (all?) web browsers display the domain name in all lower-case in the address bar. To my knowledge you cannot change this behavior via changing what you return in your HTTP response.
From RFC 4343:
According to the original DNS design decision, comparisons on name
lookup for DNS queries should be case insensitive.
From Wikipedia:
Domain names are interpreted in case-independent manner.
The browsers all seem to prefer lower-case presentation.

301 redirects with Joomla on IIS

I'm running Joomla on IIS. I've got about a dozen categories (financial newsletter publishers) that I'm using to organize about 40 articles (financial newsletters). I'm using the joomla built-in SEO so the URL's look like this:
http://www.global-autotrading.com/autotraded-newsletters/13-angel-publishing/43-options-trading-pit.html
The numbers in front of the categories and articles are annoying, and I'm not too fond of the navigation provided by a Section Layout menu item. Also, some financial newsletters don't operate under the umbrella of a publisher so I want a more flexible organization.
I've tried simply constructing a menu hierarchy (under the autotraded newsletters menu) that has some newsletters directly under the parent menu item, and some publishers with their newsletters as menu items underneath them. However, that was causing some links to break; clicking on a link would take me to the wrong article, and what not. Thus, it seems like using a hand-coded menu structure is not compatible with using another, "parallel" section-layout view of the content.
Thus, I've decided to get rid of the idea of using categories to organize that content. I'm going to create an article for each "publisher" category. I'll manually add links to each publisher's newsletters in that publisher's article. I'll also create a parallel menu structure like I was describing above.
Anyway, that's a lot of background info, with the hope that I'll get some confirmation that I'm not doing something fundamentally flawed.
The problem is that there are external sites linking directly to some URLs like above. I don't want these links to break (classic SEO problem, I believe). I think the solution is to use 301 redirects to (for example) redirect from:
http://www.global-autotrading.com/autotraded-newsletters/13-angel-publishing/43-options-trading-pit.html
to
http://www.global-autotrading.com/autotraded-newsletters/angel-publishing/options-trading-pit.html
or from
http://www.global-autotrading.com/autotraded-newsletters/4-10-percent-per-month/12-10-percent-per-month.html
to
http://www.global-autotrading.com/autotraded-newsletters/10-percent-per-month.html
There are various guidelines around for creating 301 redirects in IIS (ex: http://www.webconfs.com/how-to-redirect-a-webpage.php), but I was wondering if these are compatible with Joomla, particularly with Joomla with the SEO features turned on.
Also, if it seems like I'm doing something fundamentally wrong, please let me know :)
Thanks!
Here is the rewrite section of a web.config file that works. The trickiest part was to figure out that the redirect rules need to preceed the SEO rules in the web.config
<rewrite>
<rewriteMaps>
<rewriteMap name="StaticRedirects">
<add key="/old-url-1.html" value="new-url-1.html" />
<add key="/old-url-2.html" value="new-url-2.html" />
</rewriteMap>
</rewriteMaps>
<rules>
<rule name="Security Rule" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions logicalGrouping="MatchAny">
<add input="{QUERY_STRING}" pattern="mosConfig_[a-zA-Z_]{1,21}(=|\%3D)" ignoreCase="false" />
<add input="{QUERY_STRING}" pattern="base64_encode.*\(.*\)" ignoreCase="false" />
<add input="{QUERY_STRING}" pattern="(\<|%3C).*script.*(\>|%3E)" />
<add input="{QUERY_STRING}" pattern="GLOBALS(=|\[|\%[0-9A-Z]{0,2})" ignoreCase="false" />
<add input="{QUERY_STRING}" pattern="_REQUEST(=|\[|\%[0-9A-Z]{0,2})" ignoreCase="false" />
</conditions>
<action type="CustomResponse" url="index.php" statusCode="403" statusReason="Forbidden" statusDescription="Forbidden" />
</rule>
<rule name="Redirect Rule" stopProcessing="false">
<match url=".*" />
<conditions>
<add input="{StaticRedirects:{REQUEST_URI}}" pattern="(.+)" />
</conditions>
<action type="Redirect" url="{C:1}" appendQueryString="False" redirectType="Permanent" />
</rule>
<rule name="SEO Rule">
<match url="(.*)" ignoreCase="false" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" pattern="" ignoreCase="false" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" pattern="" ignoreCase="false" />
<add input="{URL}" negate="true" pattern="^/index.php" ignoreCase="false" />
<add input="{URL}" pattern="(/|\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/[^.]*)$" />
</conditions>
<action type="Rewrite" url="index.php" />
</rule>
</rules>
</rewrite>
Code similar to this was recently included in the default Joomla install starting at version 1.6.2.
It is important that all external redirects are listed before any internal rewrites, otherwise the rewritten pointer will be inadvertently exposed back on to the web as a new URL.

Resources