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 have a post type with the slug 'open-tour', so all its pages are under www.domain.com/open-tour/. I need all the pages under this "directory" to go to HTTPS (for credit card payments).

I can redirect a single URL okay, but the dynamic stuff goes a bit over my head. Maybe it's also because I don't know how/where to write the redirect in relation to Wordpress's htaccess block.

Anyway here's the latest iteration of my fail code:

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{http_host} ^www.domain.com/open-tour/(.*) [nc]
RewriteRule ^(.*)$ https://www.domain.com/open-tour/$1 [r=301,nc] 

Can anyone tell me what I'm doing wrong here?

EDIT: Thanks JMa and Pothi! Now, is there a way to make another rule that is the reverse of this? That is, if the user is viewing a page that's NOT in the open-tour directory, then redirect to HTTP? I tried the following code, but it didn't work. Is says "Firefox has detected that the server is redirecting the request for this address in a way that will never complete."

RewriteEngine On
RewriteBase / 
RewriteCond %{HTTPS} on 
RewriteRule !^open-tour/(.*) https://%{SERVER_NAME}/open-tour/$1 [R,L]

I figured it would be a simple matter of adding the exclamation point, but I guess not. Sorry I wrote this in a comment below, but it wasn't formatted very nice :P This whole thing wouldn't be an issue, but unfortunately once you get to the HTTPS page and go elsewhere on the site, the HTTPS sticks. I have a Constant Contact plugin that pulls up a warning for trying to pull unencrypted data to an encrypted https protocol.

share|improve this question
    
Your code doesn't work because HTTP_HOST can only contain a hostname. To specify the path as rewrite condition, you may use REQUEST_URI, like RewriteCond %{REQUEST_URI} ^/open-tour. –  Pothi Jun 25 '13 at 8:24

2 Answers 2

up vote 1 down vote accepted

Try this instead:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !=on
RewriteRule ^open-tour/(.*) https://%{SERVER_NAME}/open-tour/$1 [R,L]
share|improve this answer
1  
Your code would work, only if RewriteBase / is set. Also, it doesn't work for sitename.com/open-tour (without the trailing slash). –  Pothi Jun 25 '13 at 8:20
    
Right. I've edited code. –  JMau Jun 25 '13 at 8:45
    
That works great, thanks! Is there a way to make another rule that is the reverse of this? That is, if the user is viewing a page that's NOT in the open-tour directory, then redirect to HTTP? I tried this, but it didn't work. Is says "Firefox has detected that the server is redirecting the request for this address in a way that will never complete." RewriteEngine On RewriteBase / RewriteCond %{HTTPS} on RewriteRule !^open-tour/(.*) https://%{SERVER_NAME}/open-tour/$1 [R,L] –  James Jun 25 '13 at 16:21

To let other pages to be viewed only on HTTP, you may try this complete code...

RewriteEngine On
RewriteBase /

RewriteCond %{HTTPS} !=on
RewriteRule ^open-tour/?(.*) https://%{SERVER_NAME}/open-tour/$1 [R,L]

RewriteCond %{HTTPS} on
RewriteCond %{REWRITE_URI} !^open-tour
RewriteRule ^(.*) http://%{SERVER_NAME}/$1 [R,L]
share|improve this answer
    
Thanks for the tip, but unfortunately that causes a redirect error :( –  James Jun 26 '13 at 15:27

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.