Question

I'm working on a website locally that contains https links, so when I click on one of these links it takes me to https://localhost/... which doesn't exist as there is no SSL certificate installed.

Is there anything I can add to .htaccess that checks if I'm on localhost and if so redirects https to http? The .htaccess file is used in both local development and production on our server so anything I add mustn't affect the live website.

Was it helpful?

Solution

Nope.

Reason is any rules you put in your htaccess file will only get applied once the request has been made, which means it'll only get applied once the https://localhost/ request is made. If there's nothing listening to the HTTPS port (443) on localhost, then the rules will never get applied.

If port 443 is listening for requests on localhost, but it's just a matter of lacking an SSL certificate, the rules still won't get applied until after the SSL handshake is performed, which means, you're still going to get the security warning about your certificate.

That being said, the rules you'd want if you had apache listening to port 443 and have a certificate installed is this:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^localhost$ [NC]
RewriteCond %{HTTPS} on
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [L,R]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top