Htaccess file turn on https and developing locally

I’m building my website locally on my Mac, using MAMP, before making it live. I’ve added the following code to my .htaccess file to add www and turn on https in same rule.

RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]

But now I can’t view my site in my browser. I get the following error message:

# This site can’t provide a secure connection

**www.localhost** sent an invalid response.

ERR_SSL_PROTOCOL_ERROR

I presume my local MAMP isn’t “secure” https and so hence the error message? Does this mean that I’ll have to remove this from my .htaccess file and remember to add it before I upload my site to my server?

Yes, you would need to setup local SSL in MAMP (not sure how to do that personally but its probably in the MAMP documenation).

I use the following rules in my .htacess which switches to https and removes www but only if the site is not running on localhost. You can adapt it slightly to force the WWW.

RewriteCond %{HTTP_HOST} !=localhost
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]

i think probably you just need to change the second line to

RewriteCond %{HTTP_HOST} !^www\. [NC,OR]

Can I clarify something. Your code will work when working locally and also when live on a server? So I can use the same code regardless of the site being local or live?

Yes thats right, no messing about the htacess on uploading the site. The above rules only kick if the website is not running locally. You can just leave them in the .htacess file not worry about it when you upload.

Sounds just what I’m looking for.

I’ve added the following to the Kirby htaccess file after line 9 (this is the only change I’ve made to the Kirby file that comes with the Plain Kit):

RewriteCond %{HTTP_HOST} !=localhost
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]

But I then get the following error message on my local browser.

# This site can’t provide a secure connection

**localhost** sent an invalid response.

ERR_SSL_PROTOCOL_ERROR

So it seems to be wanting to turn on the https locally.

This isn’t working for me. I’m running the site locally and the htaccess file is trying to add https. I get the following message in my browser:

Safari can’t open the page “https://localhost:8888” because Safari can’t establish a secure connection to the server “localhost”.

This is the code inside my htaccess file:

# Kirby .htaccess
# revision 2020-06-15

# rewrite rules
<IfModule mod_rewrite.c>

# enable awesome urls. i.e.:
# http://yourdomain.com/about-us/team
RewriteEngine on

RewriteCond %{HTTP_HOST} !=localhost
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]

# make sure to set the RewriteBase correctly
# if you are running the site in a subfolder;
# otherwise links or the entire site will break.
#
# If your homepage is http://yourdomain.com/mysite,
# set the RewriteBase to:
#
# RewriteBase /mysite

# In some environments it's necessary to
# set the RewriteBase to:
#
# RewriteBase /

# block files and folders beginning with a dot, such as .git
# except for the .well-known folder, which is used for Let's Encrypt and security.txt
RewriteRule (^|/)\.(?!well-known\/) index.php [L]

# block all files in the content folder from being accessed directly
RewriteRule ^content/(.*) index.php [L]

# block all files in the site folder from being accessed directly
RewriteRule ^site/(.*) index.php [L]

# block direct access to Kirby and the Panel sources
RewriteRule ^kirby/(.*) index.php [L]

# make site links work
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php [L]

</IfModule>

# pass the Authorization header to PHP
SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1

# compress text file responses
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE application/json
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
</IfModule>

Am I doing something wrong?

Try clearing your browsers cache as sometimes it caches going to the secrure version of the site from previous attempts.

I’ve tried that… I’ll try again

Still not working.

Without adding the code to the htaccess file I can view my website in the browser at http://localhost:8888

But when I add the code it is redirecting this URL to https://localhost:8888 and I can’t view my website.

So the code, even though I am building locally, is adding https. If the code works for you, then why not me? Is this something to do with being on a Mac and using MAMP?

I think its because mamp uses port 8888 rather than the usual 80.

A quick google suggested trying this, which should take the portnumber into account.

RewriteCond %{HTTP_HOST} !^localhost(?::\d+)?$ [NC]
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]

Thank you so much. I don’t understand any of it, but it works! Thanks

I’ve just uploaded my site to my server and all the links to internal pages are broken. I think I’ve identified the problem. It is opening pages without the www for example:

https://xyz.co.uk/about

and coming back with a 404 Not Found error message.

It looks like my htaccess code isn’t working (I’d like my URLs to include ‘www’). This is what I have:

RewriteCond %{HTTP_HOST} !^localhost(?::\d+)?$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]

Comparing the above code with this code (Moving from .html to Kirby .php and SEO - #9 by sebastiangreger)

then should it be the following, to turn on https and www BUT only when live and not locally?

RewriteCond %{HTTP_HOST} !^localhost(?::\d+)?$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]

Update: on first look, this seems to work. I’ll test it more extensively.

1 Like