Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use .htaccess to redirect HTTP to HTTPs

Please, don't recommend me the long and very detailed thread with more than 173 upvotes. It didn't work for me. I have also tried many others (1, 2, 3, 4). They all give me TOO_MANY_REDIRECTS or error 500. So, here is my issue:

With my current .htaccess, this is what happens:

https://www.dukescasino.com/ - works perfectly

https://dukescasino.com/ - redirects to the above which is great

The two options below loads fine, but it should be redirecting to the https version:

http://www.dukescasino.com/

http://dukescasino.com/

Here is the current .htaccess:

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

I don't believe it is relevant, but if so, here is the list of current active plugins:

  • Advanced Custom Fields
  • All In One SEO Pack
  • Bop Search Box Item Type For Nav Menus
  • Contact Form 7
  • Disable Comments
  • Google XML Sitemaps
  • Jetpack by WordPress.com
  • Search & Filter
  • Slider WD
  • TablePress
  • UpdraftPlus - Backup/Restore
  • Wordfence Security
  • WPide
  • WP Smush
  • WP Super Cache

Edit 1 - Tests performed:

Test A:

<IfModule mod_rewrite.c> RewriteEngine On RewriteBase /  RewriteCond %{HTTPS} off # First rewrite to HTTPS: # Don't put www. here. If it is already there it will be included, if not # the subsequent rule will catch it. RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] # Now, rewrite any request to the wrong domain to use www. RewriteCond %{HTTP_HOST} !^www\. RewriteRule .* https://www.%{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> 

Result: ERR_TOO_MANY_REDIRECTS

Test B:

<IfModule mod_rewrite.c> RewriteEngine On RewriteBase /  RewriteCond %{HTTPS} !=on RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]  # BEGIN WordPress RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> 

Result: ERR_TOO_MANY_REDIRECTS

Test C:

<IfModule mod_rewrite.c> RewriteEngine On RewriteBase /  RewriteCond %{SERVER_PORT} ^80$  RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]  # BEGIN WordPress RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> 

Result: ERR_TOO_MANY_REDIRECTS

Test D:

<IfModule mod_rewrite.c> RewriteEngine On RewriteBase /  RewriteCond %{HTTPS} off RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]  # BEGIN WordPress RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> 

Result: ERR_TOO_MANY_REDIRECTS

Test E:

<IfModule mod_rewrite.c> RewriteEngine On RewriteBase /  RewriteCond %{HTTP_HOST} !^www\. RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI}$1 [R=301,L] RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}$1  # BEGIN WordPress RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> 

Result: 302 found. Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.

like image 903
Cristiano Maia Avatar asked Aug 17 '15 11:08

Cristiano Maia


People also ask

Does .htaccess do redirect?

Use a 301 redirect . htaccess to point an entire site to a different URL on a permanent basis. This is the most common type of redirect and is useful in most situations. In this example, we are redirecting to the "example.com" domain.

Should you redirect HTTP to HTTPS?

It's a perfectly acceptable "bootstrap" method - 301 redirect from HTTP to HTTPS then on the HTTPS side return a Strict-Transport-Security header in order to lock the browser into HTTPS.


2 Answers

On Dreamhost, this worked:

<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> 
like image 81
Jason Shah Avatar answered Sep 27 '22 20:09

Jason Shah


Problem solved!

Final .htaccess:

<IfModule mod_rewrite.c> RewriteEngine On RewriteBase /  RewriteCond %{ENV:HTTPS} !=on RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]  # BEGIN WordPress RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> 
like image 21
Cristiano Maia Avatar answered Sep 27 '22 19:09

Cristiano Maia