Home » Wordpress » Force HTTP Except Specific Pages in htaccess (SEO)

Force HTTP Except Specific Pages in htaccess (SEO)

Posted by: admin November 30, 2017 Leave a comment

Questions:

I am trying to search engine optimize an E-Commerce website. The website uses the WooCommerce plugin on a WordPress platform. In addition, it has an authorize.net gateway to allow users to pay directly on site. This means that the site requires an SSL certificate.

For SEO reasons I want / need the site to operate on the HTTP protocol, with the exception of the payment gateway pages (/cart, /my-account, /checkout, etc…), which should direct to the HTTPS protocol.

Currently I have the following code:

RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/cart/
RewriteCond %{REQUEST_URI} !^/checkout/

RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

With this code I’m running into a few issues. First is that when I access the “Cart” page it does not redirect to https, however the checkout page does.

Second, When the checkout page does load, it does not load some of my hosted content (Css, JS, img, etc…). I’m guessing this is because they are not operating on the same https protocol. How do I fix this?

Third, When I access the site manually using the https://mydomain.com/somepage, it is not 301 redirecting me to the http://mydomain.com/somepage version.

All of this leads me to believe that my code above is not written correctly. Does anyone have any ideas? The SEO is important, therefore the only page that should ever be displayed is the http:// page (with the exception of the ones I specify). With the https versions being 301 redirected to http://

Here is what my htaccess file looks like now –

# 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

RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/cart/
RewriteCond %{REQUEST_URI} !^/checkout/

RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

<FilesMatch "^(wp-comments-post\.php)">
Order Allow,Deny
Deny from xx.xxx.xx.
Deny from xx.xxx.xx.
Deny from xx.xxx.xx.
Allow from all
</FilesMatch>
Answers:

Put the SSL/non-SSL redirects before your wordpress rules. You also need rules to not only redirect to HTTPS, but redirect from HTTPS to HTTP:

RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/cart/
RewriteCond %{REQUEST_URI} !^/checkout/
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTPS} off
RewriteRule ^(cart|checkout)/ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]


# 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