exclude folder or directory from rewrite

How to Exclude Folder from Rewrite Rule in .htaccess

Apache allows you to rewrite URLs and redirect web pages to another locations using mod_rewrite and .htaccess. However, sometimes you may need to exclude one or more folders from redirect or exclude directory from mod_rewrite. Here is how to exclude folder from rewrite rules in .htaccess.


How to Exclude Folder from Rewrite Rule in .htaccess

Here are the steps to exclude folder from rewrite rule in .htaccess.


1. Enable mod_rewrite

If you have already enabled mod_rewrite in your Apache server, you can skip this step. Otherwise run the following commands to enable mod_rewrite, depending on your Linux

Ubuntu/Debian

$ sudo a2enmod rewrite

Redhat/CentOS/Fedora

Open Apache configuration file in a text editor.

$ sudo vi /etc/apache2/httpd.conf

Uncomment the following line by removing # at its beginning

#LoadModule rewrite_module modules/mod_rewrite.so

So it wil look like.

LoadModule rewrite_module modules/mod_rewrite.so

If you don’t find the above line, add it to your configuration file. Look for the following lines and change AllowOverride directive from None to All.

. . .
<directory var="" www="" html="">
. . .
 # 
 # AllowOverride controls what directives may be placed in .htaccess files.
 # It can be "All", "None", or any combination of the keywords:
 # Options FileInfo AuthConfig Limit
 #
 AllowOverride All
. . .
</directory>
. . .

Also read : How to Count Concurrent Connections in Apache


2. Exclude directory from rewrite rule

Create .htaccess at /var/www/html. If you have already created it, then open it in a text editor.

$ sudo vi /var/www/html/.htaccess

Let us say you want to exclude folder1, folder2 from rewrite rules then add the following line before your other rules.

RewriteCond %{REQUEST_URI} !^/(folder1|folder2/.*)$
OR
RewriteRule ^(folder1|folder2)($|/) - [L]

In both the above lines, Apache will check if the requested URL begins with /folder1 or /folder2 and not redirect them.

For example, here is the default wordpress configuration that redirects all pages with index.php to URLs without index.php.

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

Let us say you want to exclude data and admin folders from the above rewrite rules. In this case, update the above configuration as

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/(data|admin/.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

OR

RewriteEngine On
RewriteBase /
RewriteRule ^(data|admin)($|/) - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

Also read : How to Generate Subdomains on the fly


3. Restart Apache Server

Restart Apache server to apply changes.

$ sudo service apache2 restart

That’s it. Now if you visit URLs beginning with /data or /admin they will not be redirected.

Also read : How to Check What User Apache is Running As


Leave a Reply

Your email address will not be published. Required fields are marked *