skip to Main Content

I want to achieve the following with htaccess for my domain ‘example.com’:

  1. Always add ‘https://’ before the domain name or change ‘http://’ into ‘https://’;
  2. Add prefix ‘www.’ before the domain name (after ‘https://’) if no prefix is used, and do not change other prefixes (like ‘mobile.’).

So ‘example.com’ or ‘http://example.com’ will change into: ‘https://www.example.com’ and
‘mobile.example.com’ or ‘http://mobile.example.com’ will change into ‘https://mobile.example.com’.

2

Answers


  1. your .htaccess file should be :

    <IfModule mod_rewrite.c>
        <IfModule mod_negotiation.c>
            Options -MultiViews -Indexes
        </IfModule>
    
        RewriteEngine On
    
        # Start with www
        #RewriteCond %{HTTP_HOST} !^www. [NC]
        #RewriteCond %{HTTP_HOST} ^(?:www.)?(.+)$ [NC]
        #RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]
    
        # force http to https
        RewriteCond %{HTTP:X-Forwarded-Proto} !https
        RewriteCond %{HTTPS} off
        RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    
        # Redirect Trailing Slashes If Not A Folder...
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)/$ /$1 [L,R=301]
    
    
        # Handle Front Controller...
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^ index.php [L]
    
        # Handle Authorization Header
        RewriteCond %{HTTP:Authorization} .
        RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
    
    </IfModule>
    
    Login or Signup to reply.
  2. I use this simple .htaccess file to change http -> https and adding www if not set

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^example.com [nc]
    RewriteRule (.*) https://www.example.com/$1 [R=301,L]
    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search