Thread

Force website to use HTTPS using htaccess

April 28, 2016 11:37am

Subscribe [2]
  • #1 / Apr 28, 2016 11:37am

    I know this topic has come up a lot in the past, but I can’t find a solution that works.

    I have installed EE in a sub-directory on the domain /database as I’m building a database tool separate from their website.

    How do I force everything in this directory to use https:// with the .htaccess file, as well as removing the index.php from the URL?

    Thanks a lot.

  • #2 / Apr 29, 2016 6:34am

    Rob (bluedreamer)'s avatar

    Rob (bluedreamer)

    2792 posts

    This is what I normally use, replace example.com with your domain name:

    <IfModule mod_rewrite.c>
            RewriteEngine On
            RewriteBase /
    
            # Force https
            RewriteCond %{SERVER_PORT} 80
            RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
    
            # Removes index.php from ExpressionEngine URLs
            RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
            RewriteCond %{REQUEST_URI} !/system/.* [NC]
            RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
    
            # Directs all EE web requests through the site index file
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteRule ^(.*)$ /index.php/$1 [L]
    </IfModule>
  • #3 / Apr 29, 2016 7:22am

    Thanks - I’ve just tested this, but if I replace ‘https’ with ‘http’ in the URL bar of the browser, it doesn’t force the page back to ‘https’.

  • #4 / Apr 29, 2016 8:01am

    Rob (bluedreamer)'s avatar

    Rob (bluedreamer)

    2792 posts

    That’s a pretty generic example that works on most servers. Did you include or exclude the www. in the URL?

    Might be worth dropping your host a line just in case you need a variation of the rule.

  • #5 / Apr 29, 2016 8:22am

    Yeah I did include the ‘www’. Should I leave that off?

    Bearing in mind I’m running EE from a sub-directory called ‘database’, do I need to reflect anywhere in the htaccess?

  • #6 / Apr 29, 2016 8:53am

    Rob (bluedreamer)'s avatar

    Rob (bluedreamer)

    2792 posts

    Use the www if you need it, best to match what’s EE’s domain is set to in settings.

    I’m not htaccess guru but try removing “RewriteBase /” or changing to “RewriteBase /yoursubdirectoryname”

  • #7 / Apr 29, 2016 9:48am

    I think I have stumbled across the solution - although I’m not sure if it’s wholly correct.

    Suggestions welcome anyone in the know. grin

    <IfModule mod_rewrite.c>
     RewriteEngine On
     RewriteBase /
    
    # Force https
     RewriteCond %{ENV:HTTPS} !=on
     RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    
    # Removes index.php from ExpressionEngine URLs
        RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
        RewriteCond %{REQUEST_URI} !/system/.* [NC]
        RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
    
    # Directs all EE web requests through the site index file
     RewriteCond $1 !\.(gif|jpe?g|png|htm|html)$ [NC] # Show these file types without going through Expression Engine first
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ /database/index.php?/$1 [L]
    
    </IfModule>
  • #8 / Apr 30, 2016 11:58am

    Rob (bluedreamer)'s avatar

    Rob (bluedreamer)

    2792 posts

    Best thing to do is test it!

  • #9 / May 03, 2016 4:15am

    Yes I’ve already tested it and it works. Was just wondering if anyone could spot any holes in my code. grin

  • #10 / Sep 30, 2016 8:48am

    And here’s a very similar version of the same code, which is working on another site of mine:

    #non-www to www
    RewriteCond %{HTTP_HOST} !^www\.domainname\.co.uk$
    RewriteRule (.*) https://www.domainname.co.uk/$1 [R=301,L]
    
    # Force HTTPS
    RewriteCond %{HTTPS} !=on
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    
    # Removes index.php from ExpressionEngine URLs
    RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
    RewriteCond %{REQUEST_URI} !/system/.* [NC]
    RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
  • #11 / Nov 03, 2017 4:36am

    And for the record, here’s another solution which seems to work on another site I’ve been struggling with (thanks to EllisLab for help):

    RewriteCond %{ENV:HTTPS} !on [NC]
    RewriteRule ^(.*)$ https://www.domainname.com/$1 [R,L]
    
    RewriteCond %{ENV:HTTPS} on [NC]
    RewriteCond %{HTTP_HOST} ^domainname\.com$ [NC]
    RewriteRule ^(.*)$ https://www.domainname.com/$1 [R,L]

    Ahhhhh ‘joy’ of htaccess and Redirects!

ExpressionEngine News

#eecms, #events, #releases