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