htaccess RewriteCond, Multiple Conditions for RewriteRule

18,532

Solution 1

I figured it out, though I'm not sure why this works:

I switched the rules around so they happened in reverse order. Code that works is as follows:

RewriteCond %{REQUEST_URI} !\.(css|gif|jpg|png|ico|txt|xml|js|pdf|htm|zip)$
RewriteRule !(specialexception)+\.php)$ /path/to/index.php [NC,L]

The problem with this solution: Since "specialexception.php" is listed as the last rule, in the actual RewriteRule line, it only solves the problem for ONE file exception.

One messy way around this might be to use the pipe in the last line of regex:

RewriteCond %{REQUEST_URI} !\.(css|gif|jpg|png|ico|txt|xml|js|pdf|htm|zip)$
RewriteRule !(specialexception|anotherexception|foo|bar)+\.php)$ /path/to/index.php [NC,L]

If anyone has a better idea on how to add multiple exceptions for multiple files, I'd love to know!

Solution 2

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_URI} !(specialexception)\.php$
    RewriteCond %{REQUEST_URI} !(anotherexception)\.php$
    RewriteRule !\.(css|gif|jpg|png|ico|txt|xml|js|pdf|htm|zip)$ index.php [NC,L]
</IfModule>

this works fine for me. If your .htaccess lies in the same directory like index.php don't use the path from webroot. Instead use the path relative to your .htaccess-file.

Share:
18,532
Jay
Author by

Jay

Software Developer (Web), Designer, Painter, Photographer, Foodie, Dog lover.

Updated on June 04, 2022

Comments

  • Jay
    Jay almost 2 years

    I use .htaccess Mod Rewrite to send the URL to index.php, which then parses the URL and builds each page of my website. This works perfectly fine, allowing me to easily manage clean URLs using PHP instead of .htaccess.

    Ex: http://domain.com/some/url/here/ -> goes to index.php, which loads a page with multiple PHP files put together

    However, I'm trying to allow certain PHP files to load as regular PHP files (without sending the URL to index.php to parse).

    Ex: http://domain.com/some/url/here/ still works as mentioned above. http://domain.com/specialexception.php will load as a regular php file, without sending to index.php

    I have the following code:

    <IfModule mod_rewrite.c>
    
        RewriteEngine On
    
        RewriteCond $1 !^specialexception\.php$ [NC]
        RewriteRule !\.(css|gif|jpg|png|ico|txt|xml|js|pdf|htm|zip)$ /path/to/index.php [NC,L]
    
    </IfModule>
    

    However, the RewriteCond line is simply ignored right now.

    I would appreciate any help/ideas!

    Thank you.

  • Jay
    Jay over 12 years
    The .htaccess file I'm working with is saved in a subfolder. The webroot does have an htaccess file as well, but should be overwritten by the RewriteRules in the subfolder's htaccess file. The subfolder htaccess file is saved in something like /path/to/.htaccess