Excluding certain pages from being redirected to https

864 views Asked by At

I'm using the following rules on my WordPress website to:

  1. Redirect all http pages to https
  2. Redirect the careers page to http

    <IfModule mod_rewrite.c>
    RewriteEngine On
    # Go to https if not on careers
    RewriteCond %{SERVER_PORT} 80 
    RewriteCond %{REQUEST_URI} !^/careers$ [NC]
    RewriteRule ^(.*)$ https://www.mywebsite.com/$1 [R,L]
    
    # Go to http if you are on careers
    RewriteCond %{SERVER_PORT} !80 
    RewriteCond %{REQUEST_URI} ^/careers$ [NC]
    RewriteRule ^(.*)$ http://www.mywebsite.com/$1 [R,L]
    </IfModule>
    

The https redirection works fine; however, the careers page does not redirect to http. Any idea why?

here is what I have in wp-config.php

define('WP_HOME', 'https://' . $_SERVER['HTTP_HOST']);
define('WP_SITEURL', 'https://' . $_SERVER['HTTP_HOST']);
1

There are 1 answers

0
anubhava On BEST ANSWER

Try using THE_REQUEST instead of REQUEST_URI:

<IfModule mod_rewrite.c>
RewriteEngine On
# Go to https if not on careers
RewriteCond %{SERVER_PORT} =80 
RewriteCond %{THE_REQUEST} !/careers/[\s?] [NC]
RewriteRule ^(.*)$ https://www.mywebsite.com/$1 [R,L]

# Go to http if you are on careers
RewriteCond %{SERVER_PORT} !=80 
RewriteCond %{THE_REQUEST} /careers/[\s?] [NC]
RewriteRule ^(.*)$ http://www.mywebsite.com/$1 [R,L]
</IfModule>

THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of some rewrite rules. Example value of this variable is GET /index.php?id=123 HTTP/1.1.

Make sure these rules are placed above WP default rules.