Redirect specific WordPress pages to HTTPS
I have a WordPress site and want to redirect specific pages to HTTPS. Specifically, I want http://www.mydomain.com开发者_如何学C/?page_id=4
to be redirected to https://www.mydomain.com/?page_id=4
. The approach that I have taken is to add the following to .htaccess:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^http://www.mydomain.com/?page_id=4 [NC]
RewriteRule ^(.*)$ https://www.mydomain.com/?page_id=4/$1 [R=301,L]
But when I navigate to the page, it does not redirect. Any thoughts?
Well ... %{HTTP_HOST}
variable will be resolved to a domain name only and in your example it will be just www.mydomain.com
. You'll need more than that:
Options +FollowSymLinks -MultiViews
RewriteEngine on
# force secure version of this page
RewriteCond %{QUERY_STRING} ^page_id=(\d+) [NC]
RewriteCond %{HTTPS} =off
RewriteCond %{HTTP_HOST} =www.mydomain.com
RewriteRule ^(.*)$ https://www.mydomain.com/$1?page_id=%1 [R=301,L]
You have to compare query string, protocol and optionally domain name separately via 3 RewriteCond statements.
I have removed
/$1
from target URL -- it makes no sense. All what this rule supposed to do is to redirect to secure version of this and only this one particular URL.I'm not sure that this line is really required:
RewriteCond %{HTTP_HOST} =www.mydomain.com
. It should work fine without it.Make sure that you put this rule in appropriate place -- it should be placed BEFORE WordPress rewrite rules.
UPDATE: Alternative:
Options +FollowSymLinks -MultiViews
RewriteEngine on
# force secure version of this page
RewriteCond %{QUERY_STRING} ^page_id=(\d+)(.*) [NC]
RewriteCond %{HTTPS} =off
RewriteCond %{HTTP_HOST} =www.mydomain.com
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [QSA,R=301,L]
Thanks for all the help. I figured out a workaround. I basically hardcode a block for each page#:
Options +FollowSymLinks -MultiViews
RewriteEngine on
#force secure version of page=4
RewriteCond %{QUERY_STRING} =page_id=4 [NC]
RewriteCond %{HTTPS} =off
RewriteCond %{HTTP_HOST} =www.mydomain.com
RewriteRule ^(.*)$ https://www.mydomain.com/?page_id=4 [R=301,L]
#force secure version of page=5
RewriteCond %{QUERY_STRING} =page_id=5 [NC]
RewriteCond %{HTTPS} =off
RewriteCond %{HTTP_HOST} =www.mydomain.com
RewriteRule ^(.*)$ https://www.mydomain.com/?page_id=5 [R=301,L]
#force secure version of page=6
...
Not elegant, but it works.
One last issue. It is possible for the URLs to contain characters after the page_id. For instance: www.mydomain.com/?page_id=4&category=1. Does anyone know how to place a wildcard after the page_id=4? Thanks!
精彩评论