Jay
Jay

Reputation: 1104

htaccess RewriteCond, Multiple Conditions for RewriteRule

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.

Upvotes: 3

Views: 21028

Answers (2)

Seybsen
Seybsen

Reputation: 15572

<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.

Upvotes: 3

Jay
Jay

Reputation: 1104

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!

Upvotes: 2

Related Questions