Shane Jones
Shane Jones

Reputation: 905

Issues with HTTPS to HTTP htaccess

I'm having some issues with some .htaccess redirects only working in certain situations.

The code I have is

RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

And here are the results of my tests

https://www.example.com redirects to http://example.com

https://example.com does nothing

https://www.example.com/page-name redirects to http://example.com/page-name

https://example.com/page-name does nothing

This is a WordPress site and the .htaccess code sits above the WordPress code. I have also tried it below too and I get the same results

Upvotes: 0

Views: 80

Answers (2)

Shane Jones
Shane Jones

Reputation: 905

It looks like the site didnt have an SSL attatched to it so the

RewriteCond %{HTTPS} on

rule was not working correctly.

Because of this I went down this method to fix the redirects and make it work in all of the test cases that I mentioned in the question.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

Upvotes: 1

Joe
Joe

Reputation: 4897

Use this in your .htaccess instead:

RewriteCond %{HTTPS} =on
RewriteRule ^(.*)$ http://example.com%{REQUEST_URI} [R,L]

I'm assuming that you want www forced to not show, since you did not include it in the URL. If that is the case, include this extra condition to force it to not show:

RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} =on
RewriteRule ^(.*)$ http://example.com%{REQUEST_URI} [R,L]

Make sure you clear your cache before testing this.

Upvotes: 0

Related Questions