Home > Net >  Rewrite .htaccess all files except
Rewrite .htaccess all files except

Time:01-02

I currently have the following in my .htaccess file which rewrites all files names so I can limit access to my media.

#RewriteCond %{REQUEST_FILENAME} -s
#RewriteRule ^wp-content/uploads/(.*)$ dl-file.php?file=$1 [QSA,L]

However, I would like specific files to be ignored. For example: ^wp-content/uploads/2022/12/image-26.png and ^wp-content/uploads/2022/11/horse47.jpg

How can I update my rewrite rules to skip those specific files (and others).

I tried rewriting the original files after the above (3rd line) but does not work.


Solved thank you. Mr White

CodePudding user response:

I tried rewriting the original files after the above (3rd line) but does not work.

"After" is too late, the request will have already been rewritten to your script! You would need to rewrite the files before your existing rule.

For example:

RewriteRule ^wp-content/uploads/2022/12/image-26\.png$ - [L]
RewriteRule ^wp-content/uploads/2022/11/horse47\.jpg$ - [L]

# Existing directives go here...

The L flag prevents the following rules from being processed.

Don't forget to backslash-escape the literal dot and include the end-of-string anchor on the regex.

Alternatively, add exceptions (conditions) to the existing rule. For example:

RewriteCond $1 !^2022/12/image-26\.png$
RewriteCond $1 !^2022/11/horse47\.jpg$
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^wp-content/uploads/(.*) dl-file.php?file=$1 [QSA,L]

The ! prefix negates the expression. So it is successful when it does not match.

  • Related