We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Force website to use HTTPS using htaccess

How Do I?

IC360 (Oliver Cannell)'s avatar
IC360 (Oliver Cannell)
241 posts
9 years ago
IC360 (Oliver Cannell)'s avatar IC360 (Oliver Cannell)

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.

       
Rob Allen's avatar
Rob Allen
2,950 posts
9 years ago
Rob Allen's avatar Rob Allen

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>
       
IC360 (Oliver Cannell)'s avatar
IC360 (Oliver Cannell)
241 posts
9 years ago
IC360 (Oliver Cannell)'s avatar IC360 (Oliver Cannell)

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’.

       
Rob Allen's avatar
Rob Allen
2,950 posts
9 years ago
Rob Allen's avatar Rob Allen

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.

       
IC360 (Oliver Cannell)'s avatar
IC360 (Oliver Cannell)
241 posts
9 years ago
IC360 (Oliver Cannell)'s avatar IC360 (Oliver Cannell)

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?

       
Rob Allen's avatar
Rob Allen
2,950 posts
9 years ago
Rob Allen's avatar Rob Allen

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”

       
IC360 (Oliver Cannell)'s avatar
IC360 (Oliver Cannell)
241 posts
9 years ago
IC360 (Oliver Cannell)'s avatar IC360 (Oliver Cannell)

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

Suggestions welcome anyone in the know. 😊

<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>
       
Rob Allen's avatar
Rob Allen
2,950 posts
9 years ago
Rob Allen's avatar Rob Allen

Best thing to do is test it!

       
IC360 (Oliver Cannell)'s avatar
IC360 (Oliver Cannell)
241 posts
9 years ago
IC360 (Oliver Cannell)'s avatar IC360 (Oliver Cannell)

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

       
IC360 (Oliver Cannell)'s avatar
IC360 (Oliver Cannell)
241 posts
8 years ago
IC360 (Oliver Cannell)'s avatar IC360 (Oliver Cannell)

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]
       
IC360 (Oliver Cannell)'s avatar
IC360 (Oliver Cannell)
241 posts
7 years ago
IC360 (Oliver Cannell)'s avatar IC360 (Oliver Cannell)

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!

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.