mod_rewrite loop, redirecting http to https on certain section of wordpress blog

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

Read More
<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:UsersStephen> C:UsersStephenDownloadscurl-7.23.1-win64-ssl-sspicurl.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:UsersStephen> C:UsersStephenDownloadscurl-7.23.1-win64-ssl-sspicurl.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.

Related posts

Leave a Reply

1 comment

  1. 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.