How to redirect only some pages to https (with Sakura Server Wordpress)

Asked 2 years ago, Updated 2 years ago, 63 views

I used Sakura rental server, introduced Wordpress, and applied my own SSL.
I wanted to redirect only the contact page to https, but I couldn't solve it myself.
I typed the following in .htaccess and uploaded it to the root folder.
Both HTTPS and HTTP will fly to the top page.

RewriteEngine On
RewriteBase/

# When accessed via HTTP,
RewriteCond%{HTTP:X-Sakura-Forwarded-For}^$
# Skip only this page to HTTPS
RewriteCond%{REQUEST_URI}^/contact/.*$
RewriteRule^.*$https://www.example.com% {REQUEST_URI} [R,L]

If you enter so far, contact redirects to https.
If you add the code below, contact will redirect you to the front page.

# When accessed by HTTPS, click
RewriteCond%{HTTP:X-Sakura-Forwarded-For}!^$
# Skip to HTTPS other than this page
RewriteCond%{REQUEST_URI}!^/contact/.*$
RewriteRule^.*$http://www.example.com% {REQUEST_URI} [R,L] 

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

# END WordPress

Thank you for your cooperation.

wordpress .htaccess https

2022-09-30 12:06

1 Answers

I don't know if the code is correct, but I solved myself while I was searching.
There may be a smarter way to do this, but here's the code for the desired behavior:

RewriteEngine On
# If you access the contact page via http://
RewriteCond%{REQUEST_URI}^/contact/$
RewriteCond%{HTTP:X-Sakura-Forwarded-For}^$
RewriteRule^(.*)$https://%{HTTP_HOST}%{REQUEST_URI}[L,R=301]

# If you access anything other than the top page via https://
RewriteCond%{REQUEST_URI}!^/contact/$
RewriteCond%{HTTP:X-Sakura-Forwarded-For}!^$
RewriteRule^(.*)/$http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# If you access the front page via https://
RewriteCond%{HTTP:X-Sakura-Forwarded-For}!^$
RewriteRule^$ http://www.example.com/ [L,R=301]


2022-09-30 12:06

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.