Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Hi all been banging my head against a wall on this one and looked at a lot of articles.

My Task

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.

My Config

here is my .htaccess

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

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

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

The Problem

Is seems I do get redirected to https but then it loops and never shows.

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>
share|improve this question
1  
Are you sure that your apache server has mod_rewrite installed? –  mdpc Feb 18 '13 at 22:54
    
@mdpc Yes because the index.php is being rewritten. –  Shutupsquare Feb 18 '13 at 22:57
    
How are you testing whether it's working or not? –  Ladadadada Feb 18 '13 at 23:46
    
Perhaps editing the wrong file? (Yes, happens). Is the order of the stuff in the file right? –  vonbrand Feb 18 '13 at 23:58
    
Turn on Rewrite logging and post the log result if you still can't find it. Exactly how you do that depends on your version of Apache. –  Jenny D Feb 19 '13 at 7:50

2 Answers 2

Rewrite rules are processed in order and your first two rules (the index.php rules) catch every possible request. Since they both have the [L] modifier, rule processing is stopped when they match, which they always do.

The redirect rules below the index.php rules are never even tested. Try moving them to the top of the file (but after the RewriteEngine on line) and having the index.php catchall rule at the bottom.


As a side note, I highly recommend testing redirects with curl rather than using a browser. The two specific problems this avoids are cached redirects and chained redirects (This is where URL1 redirects to URL2 which redirects to URL3 and you don't ever see URL2 in a browser.)

Either curl -I <URL> or curl --include <URL> will do the job.

share|improve this answer
    
Hi first of all thank you for your valuable input but before the index.php rule the url would be /index.php/cart so do i reflect that in the rule? –  Shutupsquare Feb 19 '13 at 12:19
1  
Do you see /index.php/cart in your browser? Because if so that is why your redirects are not working. A rewrite rule must match what you actually see in the browser (or what is sent in the GET request.) If you see /cart in your browser, moving these rule to the top will work. –  Ladadadada Feb 19 '13 at 12:31
    
at the moment is see /cart I just though if I put the rule above the index.php rule then I will have an url that does not have the index.php removed yet, until it get's processed after the /cart etc rules. –  Shutupsquare Feb 19 '13 at 12:34
    
Okay I moved the rules above the index.php, and now I see it redirect to https which is good but then chrome complains that there is a redirect loop. :[ –  Shutupsquare Feb 19 '13 at 12:39
    
Don't use Chrome. That error message is exactly why. Use curl. –  Ladadadada Feb 19 '13 at 12:41

EDIT: The below is wrong - it would be valid if this were in a VirtualHost context, but since it's in .htaccess, it's not.

Now that I have a little more coffee, I see that you need to include the first / in the RewriteRule. Like this:

RewriteRule ^/secure https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
share|improve this answer
    
Thanks, this still doesn't seem to work. –  Shutupsquare Feb 19 '13 at 11:29
    
In a .htaccess file or any Directory context (such as a <Directory > block), the leading slash does not exist because the directory path (/path/to/your/directory/) has been removed. –  Ladadadada Feb 19 '13 at 12:10
    
You're right; I missed that it's in a .htaccess. –  Jenny D Feb 19 '13 at 12:31

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.