Home Adding HTTPS forcing to my .htaccess file
Reply: 1

Adding HTTPS forcing to my .htaccess file

Mike Harrison
1#
Mike Harrison Published in 2017-11-10 12:03:06Z

I have an .htaccess file the includes some rewrites that are doing a couple of things, which is working fine. It looks like this:

<IfModule mod_rewrite.c>

Options +FollowSymLinks

RewriteEngine On

# Removes .php ending from path
# ---------------

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]

# Services & Knowledgebase rewrites
# ---------------

RewriteRule ^services/([a-zA-Z0-9-/]+)/([a-zA-Z0-9-/]+)$ /services.php?cat=$1&s=$2 [L]
RewriteRule ^services/([a-zA-Z0-9-/]+)$ /services.php?service=$1 [L]

RewriteRule ^knowledge-base/([a-zA-Z0-9-/]+)/([a-zA-Z0-9-/]+)$ /knowledge-base.php?cat=$1&s=$2 [L]
RewriteRule ^knowledge-base/([a-zA-Z0-9-/]+)$ /knowledge-base.php?cat=$1 [L]

</IfModule> 

I have now added SSL to the site, and need to force HTTPS for the entire site. I have tried adding the following to my .htaccess:

# Force HTTPS
# ---------------

RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Just after the RewriteEngine On statement, however when I try example.com, the url remains as example.com and I get the following:

Not Found

The requested URL / was not found on this server.

If I go to https://example.com it works perfectly.

How do I force the https correctly, while retaining my other rewrite rules?

Thanks for any help

Mike

Nils Mielke
2#
Nils Mielke Reply to 2017-11-10 13:02:56Z

Here's what I usually use:

RewriteCond %{HTTPS} !=on
RewriteCond %{ENV:HTTPS} !=on
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

Never ran into any issues with that.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.297247 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO