1

I was trying to redirect all direct access in my subfolder using this code

RewriteEngine on

RewriteCond %{HTTP_COOKIE} !user_cookie=[^;]
RewriteRule .* http://webhost.org/ [R=301,L]

I realise it was working only for OHP files but not for other files like HTML. I tried to access the link to the PHP file and it works as I planned, but when I'm accessing the other file with .html extension it does not redirect, as if it was ignoring the condition of .htaccess.

I'm new to .htaccess. What am I doing wrong?

Server information:

  • fresh install of Vestacp
  • Apache/2.2.15 (CentOS 6 x64)
0

The code you posted doesn't necessarily apply to "direct" requests. It applies when a cookie is not set (ie. not sent back in the Cookie HTTP request header) - this is not the same thing. (Presumably you are setting a "session" cookie? However, browser's definition of a "session" varies, so these cookies can end up being more persistent than you expect.)

If it's "working only for PHP files but not for other files like HTML" then it's because the browser is not sending the Cookie HTTP request header for requests to your PHP files - for some reason. Maybe it's the PHP files that are "incorrect" and the Cookie: header should be getting sent - like for HTML files?

The difference between your PHP and HTML files could be down to caching, domain and/or URL-path. This has nothing to do with your .htaccess script.

To check for "direct access" it is more usual to check for an empty Referer header. However, this (and any) method you use is not going to be entirely reliable.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.