SISYN
SISYN

Reputation: 2269

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.

Upvotes: 2

Views: 824

Answers (2)

Luca Capuozzo
Luca Capuozzo

Reputation: 11

An alternative way is to exclude localhost from the conditions.

example:

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

Upvotes: 1

Croises
Croises

Reputation: 18671

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.

Upvotes: 2

Related Questions