Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IIS7 URL Rewriting: How not to drop HTTPS protocol from rewritten URL?

Tags:

I'm working on a website that uses IIS 7's URL rewriting feature to do a permanent redirect from example.com to www.example.com, as well as rewrites from similar domain names to the "main" one, such as from www.examples.com to www.example.com.

This rewrite rule - shown below - has worked well for some time now. However, we recently added HTTPS support and noticed that if users visit one of the URLs to be rewritten to www.example.com then HTTPS is dropped. For instance, if a user visits https://example.com they get redirected to http://www.example.com, whereas we would like them to be sent to https://www.example.com.

Here is the rewrite rule of interest (in Web.config):

<rule name="Canonical Host Name" stopProcessing="true">     <match url="(.*)" />      <conditions logicalGrouping="MatchAny">         <add input="{HTTP_HOST}" pattern="^example\.com$" />         <add input="{HTTP_HOST}" pattern="^(www\.)?example\.net$" />         <add input="{HTTP_HOST}" pattern="^(www\.)?example\.info$" />         <add input="{HTTP_HOST}" pattern="^(www\.)?examples\.com$" />     </conditions>      <action type="Redirect" url="http://www.example.com/{R:1}" redirectType="Permanent" /> </rule> 

As you can see, the action element's url attribute points directly to http://, so I get why https://example.com is redirected to http://www.example.com. My question is, how do I fix this? I tried (naively) to just drop the http:// part from the url attribute, but that didn't work.

like image 233
Scott Mitchell Avatar asked Apr 09 '10 16:04

Scott Mitchell


People also ask

What is the difference between URL Rewrite and redirect?

Simply put, a redirect is a client-side request to have the web browser go to another URL. This means that the URL that you see in the browser will update to the new URL. A rewrite is a server-side rewrite of the URL before it's fully processed by IIS.

How do I fix the URL Rewrite module in IIS?

IIS Rewrite Module ProblemUninstall the Rewrite Module from Windows Features. Go to the Web Platform Installer. Pick Url Rewrite from Products | Server section and install. Restart IIS.

What is Request_uri in URL Rewrite?

Returns exact URL what you requested. For example, if you have default.aspx file in the root and you will access your website root.


1 Answers

Here's Scott's answer with Hasan's improvements. This should cover mixed SSL/non-SSL sites. The rule basically says "if the url does not have www.example.com", do a permanent redirect to it. Essentially... you are redirecting people who visit you without www or directly to your IP address.

<rewrite> <rules>     <rule name="Canonical Host Name" stopProcessing="true">         <match url="(.*)" />         <conditions logicalGrouping="MatchAll">             <add input="{HTTP_HOST}" pattern="^www\.example\.com$" negate="true" />         </conditions>         <action type="Redirect" url="{MapSSL:{HTTPS}}www.example.com/{R:1}" redirectType="Permanent" />     </rule> </rules> <rewriteMaps>     <rewriteMap name="MapSSL" defaultValue="http://">         <add key="ON" value="https://" />         <add key="OFF" value="http://" />     </rewriteMap> </rewriteMaps> </rewrite> 
like image 83
Ralph N Avatar answered Dec 18 '22 22:12

Ralph N