RewriteRule for HTTP to HTTPS and WWW ISAPI Rewrite

711 views Asked by At

I've trawled many forums and tried many solutions. None work correctly. I am using ISAPI Rewrite 3 for IIS.

I need to change all requests to our website to WWW and HTTPS.

For example:

to all change to:

I've used http://htaccess.madewithlove.be, which may be buggy because I'm getting seemingly incorrect results for so-called working solutions. I don't want to be testing umpteen things on the live site.

This supposedly correct example (one of many) I found gives incorrect results:

RewriteEngine on
RewriteBase /
RewriteCond %{SERVER_PORT} !443
# Extract non-www portion of HTTP_HOST
RewriteCond %{HTTP_HOST} ^(www\.)?(.*) [NC]
# Redirect to HTTPS with www
RewriteRule (.*) https://www.%2/$1 [R=301]

Example tests:

Can anyone give me a set of rules that will cleanly and reliably turn any non www request to our website to the correct https://www version, and not add invalid slashes etc?

1

There are 1 answers

0
Yaroslav On

Try this one:

RewriteEngine On 

# non-www to www
RewriteCond %{HTTP_HOST} ^example\.com$ [NC] 
RewriteRule (.*) https\://www.example.com/$1 [R=301]

# HTTP to HTTPS 
RewriteCond %{HTTPS} off [NC] 
RewriteRule (.*) https\://www.example.com/$1 [R=301]