Redirect HTTP to HTTPS / SSL

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!

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;
}
3 Likes

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]

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>

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?

2 Likes

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!

Hi Clay,

I tried adding your solution in my site.conf file and somehow I forgot to save the file with my URL and now every time click on View Site from the Flywheel Local app, I get my site dev.local to display for an instant and then it redirects to https://domain_goes_here.
I have uninstalled the app and started fresh many times but I can’t seem to get rid of this redirect. If I enter my site’s URL in the browser, it displays my site fine. The redirect only occurs when I click on View Site.
Any suggestions on clearing this permanent or so it seems redirect?

Thanks very much!

1 Like