Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to rewrite 3 sections of a wordpress site to https if they are accessed over http:

/cart/

/my-account/

/checkout/

As well as these rewrite's worpress has added a rewrite to remove index.php out of the url. The index.php rewrite is about the only thing that is working. here is my .htaccess

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} (checkout|cart|my-account)
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=302,L]

# BEGIN WordPress
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress

</IfModule>

# END WordPress

Some Curl tests

In the below you can see my access the http version of /cart to be rightly told that this has moved to https /cart, So I then try the https version to be told that it has moved to the very same https version thus a loop.

PS C:\Users\Stephen> C:\Users\Stephen\Downloads\curl-7.23.1-win64-ssl-sspi\curl.exe -k -i http://www.mysite.com/cart
HTTP/1.1 302 Found
Date: Wed, 20 Feb 2013 09:07:06 GMT
Server: Apache
Location: https://www.mysite.com/cart
Vary: Accept-Encoding
Content-Length: 285
Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>302 Found</title>
</head><body>
<h1>Found</h1>
<p>The document has moved <a href="https://www.mysite.com/cart">here</a>.</p>
<hr>
<address>Apache Server at www.mysite.com Port 80</address>
</body></html>

PS C:\Users\Stephen> C:\Users\Stephen\Downloads\curl-7.23.1-win64-ssl-sspi\curl.exe -k -i https://www.mysite.com/cart
HTTP/1.1 302 Found
Date: Wed, 20 Feb 2013 09:07:06 GMT
Server: Apache
Location: https://www.mysite.com/cart
Vary: Accept-Encoding
Content-Length: 285
Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>302 Found</title>
</head><body>
<h1>Found</h1>
<p>The document has moved <a href="https://www.mysite.com/cart">here</a>.</p>
<hr>
<address>Apache Server at www.mysite.com Port 80</address>
</body></html>

It redirects to the https but then gets stuck in a loop and the browser will complain of a loop. Any rewrite guru's in here.

share|improve this question
add comment

1 Answer

I am by no means a mod_rewrite expert but would something like this work?

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

# Force HTTPS for /cart/
RewriteCond %{HTTPS} !=on
RewriteCond %{THE_REQUEST} ^[A-Z]+\s/cart [NC]
RewriteRule ^(cart) https://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L]

# Force HTTPS for /my-account/
RewriteCond %{HTTPS} !=on
RewriteCond %{THE_REQUEST} ^[A-Z]+\s/my-account [NC]
RewriteRule ^(my-account) https://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L]

# Force HTTPS for /checkout/
RewriteCond %{HTTPS} !=on
RewriteCond %{THE_REQUEST} ^[A-Z]+\s/checkout [NC]
RewriteRule ^(checkout) https://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L]

# BEGIN WordPress
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress

</IfModule>

# END WordPress

I'm sure the 3 separate statements could be combined somehow, but like I said I'm no expert. Let me know if this functions properly for you though.

share|improve this answer
    
Thanks Josh ill try this out in the morning. –  Shutupsquare Feb 19 '13 at 22:56
    
Hi Josh just tried this and I get the same loop error. –  Shutupsquare Feb 20 '13 at 9:47
    
@Shutupsquare Are there any .htaccess files in the subdirectories that might be trying to apply additional redirects? This shouldn't cause a loop since it only applies the redirect if https != on –  Josh Mountain Feb 20 '13 at 15:55
    
no more htaccess no, I have a feeling that the https is being proxied to the wordpress server as apache and php think the port is 80 when I'm on https which means one thing, the apache wodpress is running on is behind a proxy. –  Shutupsquare Feb 20 '13 at 20:34
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.