Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP to HTTPS redirect for only one URL

Was wondering if anybody could help. Thanks so much!

Problem:

  • blog.example.com
  • cool.example.com

    redirect to https://www.example.com

Goal:

We're trying to only redirect

  • http://example.com

  • http://www.example.com

    to https://www.example.com

Current code

Current code in web.config is this. Stuck on <match url="(.*)">

Below is rest of rewrite rule.

<system.webServer>     
 <rewrite>
    <rules>
       <rule name="Redirect to https">
          <match url="(.*)" />
          <conditions>
             <add input="{SERVER_PORT}" pattern="443" negate="true" />
          </conditions>
          <action type="Redirect" url="https://www.example.com" />
       </rule>
    </rules>
 </rewrite>
<modules runAllManagedModulesForAllRequests="true"/>
<defaultDocument enabled="true">
  <files>
    <clear/>    
    <!--Remove this line or place below to deprecate-->
    <add value="default.aspx"/>
    <add value="index.html"/>
    <add value="index.php"/>
    <add value="default.html"/>
  </files>
</defaultDocument>

like image 398
Danger14 Avatar asked Apr 08 '26 08:04

Danger14


2 Answers

Modify <match url="(.*)" /> to include only example.com and www.example.com - right now you're matching all urls.

http://learn.iis.net/page.aspx/461/creating-rewrite-rules-for-the-url-rewrite-module/

like image 175
Jakub Konecki Avatar answered Apr 10 '26 01:04

Jakub Konecki


Try this:

<rule name="Rewrite HTTP to HTTPS" stopProcessing="false" enabled="false">
<match url=".*" />
<conditions>
    <add input="{HTTPS}" pattern="off" />
    <add input="{HTTP_HOST}" type=”Pattern” pattern="^(www\.)?example.com$">
</conditions>
<action type="Redirect" url="https://www.example.com{REQUEST_URI}" appendQueryString="false" redirectType="Permanent" />

like image 40
David Peden Avatar answered Apr 10 '26 00:04

David Peden



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!