WebFaction
Community site: login faq

I would like my site to serve all URLs on my site as http (even if the browser requests https) outside of certain URLs, namely those that contain:

wp-admin
wp-login
abc123

I don't want to build my own Apache, so I am trying to accomplish this in .htaccess. Based on this discussion, here's what I tried already:

# BEGIN Force non-SSL outside wp-admin and certain other pages
RewriteEngine On

RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/wp-admin$
RewriteCond %{REQUEST_URI} !^/wp-login$
RewriteCond %{REQUEST_URI} !^/abc123$
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} ^/wp-admin$
RewriteCond %{REQUEST_URI} ^/wp-login$
RewriteCond %{REQUEST_URI} ^/abc123$
RewriteRule ^(.*)$ http://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

# END Force non-SSL outside wp-admin and login page

But I get an error in the browser saying that I've created an endless redirect loop.

Peter

asked 12 Aug '14, 17:01

PeterA
10431022
accept rate: 14%


The problem is that you're using %{HTTPS} for your condition, when you should instead be using %{HTTP:X-Forwarded-SSL}.

More information is available in our documentation here: Redirecting from HTTP to HTTPS

Hope that helps!

permanent link

answered 12 Aug '14, 19:24

seanf
12.2k41936
accept rate: 37%

Thanks for that tip, Sean. I tried it, but what happens is that everything ends up getting redirected to https, which is the opposite outcome of what I'm looking for. I also made some tweaks to the RewriteCond syntax but got the same problem… everything redirecting to HTTPS instead of HTTP:

RewriteEngine On

RewriteCond %{HTTP:X-Forwarded-SSL} =off
RewriteCond %{REQUEST_URI} !^\/wp-admin [OR]
RewriteCond %{REQUEST_URI} !^\/wp-login [OR]
RewriteCond %{REQUEST_URI} !^\/abc123
RewriteRule (.*) https://%{HTTP_HOST}/$1 [L,R=301]

RewriteCond %{HTTP:X-Forwarded-SSL} =on
RewriteCond %{REQUEST_URI} ^\/wp-admin [OR]
RewriteCond %{REQUEST_URI} ^\/wp-login [OR]
RewriteCond %{REQUEST_URI} ^\/abc123
RewriteRule (.*) http://%{HTTP_HOST}/$1 [L,R=301]
(12 Aug '14, 20:18) PeterA

Thanks Sean. Turns out this code does the opposite of what I want to accomplish: It converts all URLs to HTTPS, but I want to convert all URLs to HTTP. I tweaked some of the RewriteCond regex syntax, but got the same result:

RewriteEngine On

RewriteCond %{HTTP:X-Forwarded-SSL} =off
RewriteCond %{REQUEST_URI} !^\/wp-admin [OR]
RewriteCond %{REQUEST_URI} !^\/wp-login [OR]
RewriteCond %{REQUEST_URI} !^\/abc123
RewriteRule (.*) https://%{HTTP_HOST}/$1 [L,R=301]

RewriteCond %{HTTP:X-Forwarded-SSL} =on
RewriteCond %{REQUEST_URI} ^\/wp-admin [OR]
RewriteCond %{REQUEST_URI} ^\/wp-login [OR]
RewriteCond %{REQUEST_URI} ^\/abc123
RewriteRule (.*) http://%{HTTP_HOST}/$1 [L,R=301]
(12 Aug '14, 20:22) PeterA

Try combining those OR conditions into a single regex, and using "!on" as we show in our documentation:

RewriteEngine On

RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteCond %{REQUEST_URI} ^\/(wp-admin|wp-login|abc123)
RewriteRule (.*) https://%{HTTP_HOST}/$1 [L,R=301]

RewriteCond %{HTTP:X-Forwarded-SSL} =on
RewriteCond %{REQUEST_URI} !^\/(wp-admin|wp-login|abc123)
RewriteRule (.*) http://%{HTTP_HOST}/$1 [L,R=301]
(12 Aug '14, 22:05) seanf

Thanks Sean! Your last version worked perfectly:

RewriteEngine On

RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteCond %{REQUEST_URI} ^\/(wp-admin|wp-login|abc123)
RewriteRule (.*) https://%{HTTP_HOST}/$1 [L,R=301]

RewriteCond %{HTTP:X-Forwarded-SSL} =on
RewriteCond %{REQUEST_URI} !^\/(wp-admin|wp-login|abc123)
RewriteRule (.*) http://%{HTTP_HOST}/$1 [L,R=301]

I just had to make sure to insert it before # BEGIN WordPress.

permanent link

answered 13 Aug '14, 02:14

PeterA
10431022
accept rate: 14%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×75
×75
×33

question asked: 12 Aug '14, 17:01

question was seen: 13,435 times

last updated: 13 Aug '14, 02:14

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2019 SWARMA LIMITED - WEBFACTION IS A SERVICE OF SWARMA LIMITED
REGISTERED IN ENGLAND AND WALES 5729350 - VAT REGISTRATION NUMBER 877397162
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM