Exempt Sub-Directory From URI Rewrite Rules

Here's to those looking for a simple solution on how to allow a certain subfolder to be accessed by the public with an existing Rewrite Conditions. Say you have a Downloads folder along the root directory of your site which contains files for download. Problem is, any URI request pointing to this location is automatically rewritten by some existing htaccess rules and conditions.

This can easily be solved by just inserting a single line of Apache Rewrite code. First you need to identify how many of RewriteRule and RewriteCond lines are currently in your htaccess file (let's name it RULECOUNT). Note that these lines are typically grouped together which this solution is perfectly suitable. Next thing you do is place the following code right before these lines. For the purpose of this example, let's assume you have four lines of rules in your htaccess file (RULECOUNT = 4).

RewriteRule ^downloads/ - [S=RULECOUNT]

What this thing does is simply skip the rest of the Conditions and Rules if the URI is trying to invoke our Downloads directory or any of the files within it.

Example URI:

Here's a working example code:
RewriteRule ^downloads/ - [S=4]

RewriteCond %{REQUEST_URI} !(\.gif)|(\.jpg)|(\.png)|(\.css)|(\.js)|(\.php)(\.txt)$
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* sapphire/main.php?url=%1&%{QUERY_STRING} [L]

I thought of posting this topic because one of the sites we're maintaining is integrated into Silverstripe. And by Silverstripe's default setting, all of its URI goes through rewrite rules. This solution simply puts an exemption on a specific subdirectory.

Related Posts:

Bookmark and Share


Anonymous said...

Thanks, just what I needed.

Post a Comment

Hi there! Please leave your message here. Also, I may not be able to respond to your query right away. So please bear with me. Thanks. :)