DasSaffe
DasSaffe

Reputation: 2198

force single site to use https, all other http (wordpress)

I found several related questions, but non of theme seem to work, so I have to ask again:

I'm using a wordpress site and I want a single site to use https (/kontakt/). All other sites have to use the http (due to duplicate content and such).

I tried a plugin called Wordpress HTTPS which should do exactly that, but the plugin is outdated (3 years not updated) and also doesn't work as expected.

For this case, I thought about using the .htaccess. These is my htaccess-file:

My URL looks exactly like this: http://www.example.com/kontakt/

# 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]    

# END WordPress
RewriteCond %{HTTPS} off
RewriteRule ^kontakt/$ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L,NE,NC]

# change kontaktform
RewriteCond %{HTTPS}  off
RewriteRule  ^/kontakt https://www.example.com/kontakt/  [R=301,L,QSA]

RewriteCond %{HTTPS}  on
RewriteCond %{REQUEST_URI}   !^/kontakt/
RewriteRule  ^(.*)$  http://www.example.com/$1  [R=301,L,QSA]

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

</IfModule>

This attempt was taken from here:

force one page to use HTTPS and the others to use HTTP with .htaccess

but that didn't do the trick. My problem now is:

a) the site is not redirected to https (I can confirm the htaccess is working, since i can produce 500's on demand)

b) once it is on https://, all other sides stay on that protocol (already in the link-preview)

What I actually want:

IF the site = /kontakt/ --> use https

ELSE use HTTP

Upvotes: 1

Views: 96

Answers (1)

anubhava
anubhava

Reputation: 785128

Try these rules:

RewriteEngine On
RewriteBase /

# add www if missing in domain name
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=302,NE,L]

# if http and /kontakt/ then make it https
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} /kontakt/ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L,NE,NC]

# if https and NOT /kontakt/ then make it http
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} !/kontakt/ [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R=302,L,NE,NC]

# default WP rules

RewriteRule ^index\.php$ - [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

I used %{THE_REQUEST} variable for this. THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of some rewrite rules unlike %{REQUEST_URI} variable.

Clear your browser cache before testing it and also test in Chrome dev tool with caching disabled.

Upvotes: 1

Related Questions