Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & phranque

Message Too Old, No Replies

mod rewrite negative match not working

Trying to stop re-writing of actual files/directories

10:17 am on Feb 14, 2017 (gmt 0)

Senior Member

WebmasterWorld Senior Member trillianjedi is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Apr 15, 2003
votes: 3

Hi gang!

Been a while since I had one of these issues, but I'm on holiday so building something for fun as my office won't allow me to write code anymore :)

I'm playing with a REST-ful API that has a website on the same server and I want to match the first directory and map that to a .php file. I want to ignore everything else, so my static html, php, cs, js etc files remain as is. EG:-

https://example.com/123-abc/ . <-- gets mod_rewritten to https://example.com/myfile.php?q=123-abc


https://example.com/js/jquery.js <-- does not get mod_rewritten because it's an actual file that really exists

After some googling, thinking and trial and error I came up with this:-

Options +FollowSymLinks
RewriteEngine On
LogLevel alert rewrite:trace6
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /myfile.php?q=$1 [L,QSA]

I believe the RewriteRule is working correctly because the matches that I did want to catch are working.

However, all my javascript, css etc is getting matched and re-written too.

My intention/understanding was that these two lines:-

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

... would prevent from re-writing the existing files and existing directories.

I slept on this to see if I could spot my mistake in the morning, but I can't. I'm hoping someone here can :)


PS - I don't know if it makes a difference, but I haven't put this in .htaccess - I have put it in my apache default-ssl config file.
1:54 pm on Feb 14, 2017 (gmt 0)


WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
votes: 195

try this:

Options -MultiViews

i don't think this is your problem but you might also need this:

AcceptPathInfo Off

you might also want a preceding RewriteCond to exclude requests for myfile.php from being further processed by the ruleset.
7:32 pm on Feb 14, 2017 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
votes: 749

Whenever possible, avoid the !f and !d tests, since the server has to perform these tests on every single request. Instead, have something like this before the CMS-or-whatever rewrite code:

RewriteRule \.(js|css|jpg|png|ico)$ - [L]
listing any extension (case sensitive) that actually exists on your site. This will not prevent the server from returning a 404 further down the line if it turns out you goofed and some particular non-page file really doesn't exist. It will just stop mod_rewrite from getting involved.