On a wordpress site(also looking for multisite solution), looking for .htaccess to check for cookie being set, then if cookie is found add a parameter to the permalink. It works for top level, but any other permalink it wont work...been at it all day would love some input. This is Wordpress specific as I believe special consideration is needed for Wordpress that i am missing.

For instance if cookie found: http://domain.com will properly rewrite to: http://domain.com/?human=yes

However what wont work, if cookie is found: http://domain.com/sample-page/ wont re-write to http://domain.com/sample-page/?human=yes

Here is one of my attempts.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

RewriteCond %{HTTP_COOKIE} cookie|cookie2
RewriteCond %{QUERY_STRING}     !^human=yes [NC]
RewriteCond %{REQUEST_URI} !^/wp-admin/ [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteCond %{REQUEST_URI} !^/wp-login.php [NC]
RewriteRule ^([_0-9a-zA-Z-]+/)?$ $1?human=yes [R=302,L,QSA]

Appreciate the input. Looking to do this via .htaccess and not hit the php wordpress site for a faster time to load.

  • While this would be better to do in PHP, that's not how URLs work. the ?human=yes part isn't a part of the URL proper, it's a GET variable. ?human=yes is just how it gets shown to humans. So you get http://domain.com/sample-page/ with human=yes in the HTTP headers. You're going to need a http redirect to change that. Eitherway this isn't an Apache forum – Tom J Nowell Jan 6 '18 at 23:40
  • >> While this would be better to do in PHP Why? If I can do this before php has to load, and since it can be done without php wouldn't that make everything faster? I've been searching for an answer, the way wordpress rewrites things I believe is why the standard ways of doing re-write rules aren't working for subdirectories making this specific to wordpress. – bmiskie Jan 7 '18 at 0:14
  • I didn't say it was possible to do this via Apache. What you want is not a rewrite, it's a redirect. Rewrites take a users input and change where it's routed to on the server. What you want to do is change the user input if a condition is matched, which means you want to redirect the user to /?human=yes if they have a cookie. redirects and rewrites are not the same. You will never accomplish this with rewrites – Tom J Nowell Jan 7 '18 at 0:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.