0

Desired behavior:

Visiting http://www.example.com/our-services/individual-medical-dental-insurance redirects to https: https://www.example.com/our-services/individual-medical-dental-insurance

Visiting any other page via https, such as https://www.example.com/about-group-benefit-services/, redirects to non-https: http://www.example.com/about-group-benefit-services/

I've tried a variety of solutions, and this is my most recent version (which does not work):

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} our-services/individual-medical-dental-insurance
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^our-services\/individual\-medical\-dental\-insurance
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}/$1 
[L,R=301]   
</IfModule>

It may be worth noting that this is in a WordPress site, and the remainder of the .htaccess file contains this:

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

# END WordPress

(The .htaccess does not contain anything other than the above).

random_user_name
  • 25,694
  • 7
  • 76
  • 115
  • Is *any* redirecting happening at all or is there an error, or just nothing? Are you using something like cloudflare or AWS? – Jon Lin May 02 '16 at 15:45
  • No cloudflare / AWS. Redirects to non-https work properly, the redirect to https does not work - doesn't go to https, and appends the path to the end again `http://www.example.com/our-services/individual-medical-dental-insurance/our-services/individual-medical-dental-insurance` – random_user_name May 02 '16 at 15:46
  • @LoicTheAztec - that works fine, **except** once you visit the https page, the rest of the site *stays https*, which is what I'm trying to resolve. – random_user_name May 02 '16 at 15:47
  • And what about this one: [.htaccess https redirect for a single page (using relative urls)](http://stackoverflow.com/questions/9899258/htaccess-https-redirect-for-a-single-page-using-relative-urls) – LoicTheAztec May 02 '16 at 15:53
  • @LoicTheAztec - thanks - I had done a bunch of searching, and found several of these - which is what my version above is based on. There's enough differences between my version and that version that - while it **may work** - I am looking for help to make it apply to my specific instance. – random_user_name May 02 '16 at 15:59
  • Are you sure wordpress isn't the culprit? Have you tried just using a wordpress plugin to do this? Are you sure you have your rules *before* the wordpress routing rule? – Jon Lin May 02 '16 at 20:39
  • @JonLin - thanks. Positive on all counts (1) WP is working find. Strip the section that attempts to send it to `http` out, and everything works perfectly. (2) Yes - the plugin (Easy 301 Redirects) adds the rule that redirects TO https, but the issue is when you navigate away afterwards, it's not kicked back to http as it should be for the rest of the site. (3) I'm positive the rules are before the WP rules. Can document / prove if that would help. – random_user_name May 02 '16 at 20:57
  • @JonLin - you helped me with another htaccess issue - would you mind taking just a quick moment - do my rules look correct? Do (or don't I) need to escape the hypens in the rewrite condition? Is there *anything* about them that look off, or that you would try differently? I tried a HUGE number of variations before coming here, and truly am at a loss at this point. Thank you! – random_user_name May 03 '16 at 13:53

1 Answers1

1

Not sure if this will make it work but there's a few issues with your rules and it won't fit into a comment:

The %{REQUEST_URI} always starts with a "/" and thus your regex will always be true (since it'll never start with "our-services").

The other thing is that your rule is doing the appending of the URL at the end because of the $1 backreference, so you need to remove that.

Lastly, both redirects are going to https://.

Also, you don't need to escape the slashes or dashes:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/our-services/individual-medical-dental-insurance
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/our-services/individual-medical-dental-insurance
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]   
</IfModule>
Jon Lin
  • 142,182
  • 29
  • 220
  • 220