Redirect HTTP to HTTPS / SSL


#1

I have a local site that works on HTTPS with nginx.

I tried to add this code:
# BEGIN Redirect to https / SSL
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# END Redirect to https / SSL
to /…/Documents/Local Sites/app/public/.htaccess

but it’s not redirecting http://mysite.dev to https://mysite.dev

I switched the webserver to Apache and then the .htaccess rule became effective. How do I setup such a redirect on nginx?

Thanks in advance!


#2

Adding this snippet above the index line in conf/nginx/site.conf should do the trick :slight_smile:

if ($http_x_forwarded_proto != "https") {
    rewrite ^(.*)$ https://DOMAIN_GOES_HERE$1 permanent;
}

#4

Hi Clay

Whats the same requirement but for an Apache server.

I tried this in my .htaccess but get a too many redirects error:

RewriteCond %{HTTP_HOST} ^site.dev [NC,OR]
RewriteCond %{HTTP_HOST} ^site.dev [NC]
RewriteRule ^(.*)$ https://site.dev/$1 [L,R=301,NC]

#5

Hey @raison,

Check out this solution on Stack Overflow: https://stackoverflow.com/a/36475195

Here’s the .htaccess copy and pasted from the solution for convenience:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase / 

RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 

# BEGIN WordPress
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

#6

I get too many redirects with that example too.

Spent much time on this without luck today. Have you managed to get this working with your setup?

Been reading about the move away from .dev domains and how best practice to develop with https, so i suspect it might be good to have this setup like this on local as a default. What do you think?


#7

Hey there! I’m using this for Apache servers for long time

RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

Cheers!