htaccess redirect http to https keeping submains

i've been trying to work with the htaccess and went to many websites and no luck.

i need help with the following if user type the following:

  1. ->
  2. http// ->
  3. http// ->

RewriteEngine On

#Force www:
RewriteCond %{HTTPS}s ^on(s)|
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]

RewriteBase /

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

thank you!

1 answer

  • answered 2018-08-09 07:54 Joe

    You can just use this in your .htaccess:

    RewriteEngine On
    RewriteCond %{HTTPS} off [OR]
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

    This checks if the URL has:

    1. SSL enabled (HTTPs)
    2. If www is not present.

    If either of those match, it will then force HTTPs and www onto the URL. I've set this using a R=301 redirection, which is permanent. For testing purposes I advise you change this to R=302 as this is temporary.

    Make sure you clear your cache before testing this.