Search code examples
apache.htaccessssl

Prevent htaccess HTTPS forward from working on localhost


I have the following code in the my htaccess to forward all URLs to their HTTPS counterpart.

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

RewriteCond %{HTTP_HOST} ^www\.merkd\.com
RewriteRule ^(.*)$ https://merkd.com [L,NC,QSA]

How can I modify this code so that this does not break my localhost build of my site? I run it on a local Apache server at the URL http://localhost where I do not have a SSL certificate.

If possible, could you also update the code so that www.merkd.com/somepage will forward to merkd.com/somepage rather than just defaulting to merkd.com.


Solution

  • You can use:

    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteCond %{HTTP_HOST} merkd\.com [NC]
    RewriteRule ^ https://merkd.com%{REQUEST_URI} [R,L]
    
    RewriteCond %{HTTP_HOST} ^www\.merkd\.com [NC]
    RewriteRule ^ https://merkd.com%{REQUEST_URI} [R,L]
    

    I change %{HTTP_HOST} with the domain name, to avoid redirect to https and after to www.