chicky
chicky

Reputation: 135

htaccess rule not following condition

i have a htaccess file with

Options +Indexes
RewriteEngine on
RewriteCond %{HTTP_COOKIE} !mycookie [NC]
RewriteRule /*.(mp4|wav|mp3|MP3|WAV|MP4|omf|OMF|jpg|jpeg|gif)$ /noaccess.html [R=307,NC]
RewriteRule ^download\.php$ /noaccess.html [R,L]
DirectoryIndex root.php /clients/files_client.php
ErrorDocument 404 /clients/404.php
ErrorDocument 400 /clients/root.php

The second rewrite rule is always executed, independent from the condition. Any url containing download.php is redirected to noaccess.html no matter if the cookie exists or not. Why?

Upvotes: 1

Views: 243

Answers (2)

Olaf Dietsche
Olaf Dietsche

Reputation: 74048

Any RewriteCond belongs to the immediately following RewriteRule.

One or more RewriteCond directives can be used to restrict the types of requests that will be subject to the following RewriteRule.

This means, if you want a condition for more than one rule, you must duplicate it for each rule, e.g.

RewriteCond %{HTTP_COOKIE} !mycookie [NC]
RewriteRule /*.(mp4|wav|mp3|MP3|WAV|MP4|omf|OMF|jpg|jpeg|gif)$ /noaccess.html [R=307,NC]

RewriteCond %{HTTP_COOKIE} !mycookie [NC]
RewriteRule ^download\.php$ /noaccess.html [R,L]

If the condition should be valid for all rules, you may also exit the rule chain early with the opposite condition

RewriteCond %{HTTP_COOKIE} mycookie [NC]
RewriteRule ^ - [L]

RewriteRule /*.(mp4|wav|mp3|MP3|WAV|MP4|omf|OMF|jpg|jpeg|gif)$ /noaccess.html [R=307,NC]

RewriteRule ^download\.php$ /noaccess.html [R,L]

Upvotes: 1

anubhava
anubhava

Reputation: 785256

Your rules and conditions don't seem correct.

Have it like this:

DirectoryIndex root.php /clients/files_client.php
ErrorDocument 404 /clients/404.php
ErrorDocument 400 /clients/root.php
Options +Indexes
RewriteEngine on

RewriteCond %{HTTP_COOKIE} !mycookie [NC]
RewriteCond %{QUERY_STRING} \.(?:mp[43]|wav|omf|jpe?g|gif) [NC]
RewriteRule (?:^|/)download\.php$ /noaccess.html [R=307,NC,L]

Make sure to test it after clearing browser cache completely.

Upvotes: 1

Related Questions