Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to redirect all traffic to https

I want to redirect any traffic that goes to http://example.com to https://example.com

same for http://example.com/about to https://example.com/about

I thought it would be something like this:

RewriteCond %{HTTP_HOST} ^example\.com$ [NC] RewriteRule ^(.*)$ https://example.com/$1 [R=301,L] 
like image 572
Brad Avatar asked Jul 13 '10 17:07

Brad


People also ask

Should I always redirect to HTTPS?

Without SSL, your website will show insecure to the visitors. Therefore, using an SSL-encrypted connection for safety, accessibility or PCI compliance reasons is necessary. It becomes very important to redirect from HTTP to HTTPS.

How do I redirect all HTTPS requests to HTTP?

In order for something to redirect HTTPS to HTTP, something must be listening on the HTTPS port. Your client must first open a SSL/TLS connection to the port serving HTTPS, HTTP traffic is tunneled through the SSL/TLS connection and the server will respond with a redirect to the HTTP port.


1 Answers

This works for me:

RewriteEngine on RewriteCond %{HTTPS} off RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} 

If the traffic is coming in over non-SSL HTTP, then redirect to the HTTP equivalent of whatever page the user was originally trying to access. It also doesn't involve any mod_rewrite options, so it's easy to read.

Side rant: why does everyone feel the need to explicitly set the HTTP code of the redirect and mark one of their rewrites as the "last" one? Seriously, I've seen dozens of same-looking htaccess rules in just the last few days.

like image 141
Borealid Avatar answered Sep 24 '22 19:09

Borealid