Raddy
Raddy

Reputation: 303

htaccess redirect based on wordpress cookie value

I am trying to redirect users without a cookie "wordpress_logged_in" from the "wp-json/" to the base url of the website. Unfortunately when I go to the url "wp-json" nothing changes and the JSON shows as normal. When I test the script to a directory that exist on the server (folder called "test") the script works fine.

RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R,QSA]

RewriteCond %{HTTP_COOKIE} !.*wordpress_logged_in.*$ [NC]
RewriteCond %{REQUEST_URI} ^.*wp-json/wp/v2/(users|comments|posts|pages|media|types|statuses|taxonomies|categories|tags|settings) [NC] 
RewriteRule . http://%{HTTP_HOST}%1/wp-login.php?redirect_to=%{REQUEST_URI} [L,QSA]

I've tried

Incognito mode, clearing cookies/history/cache, different browsers, different web servers, I've tried creating the wp-json folder but that breaks the API

Any ideas would be much appreciated!

Upvotes: 5

Views: 979

Answers (1)

Raddy
Raddy

Reputation: 303

I figured out the "wordpress_logged_in" was changing due to a plugin that was installed. I think that solves the misery.

Upvotes: 2

Related Questions