Exempting specific files from Rewrite Engine.

I have this code right now that works...

when i type something like www.mysite.com/first_test.html 

it gives me  www.mysite.com/result.asp?kwds=first test, but it still keeps the static html(for SEO purposes)

RewriteEngine on
RewriteRule ^([0-9a-zA-Z]+)[_]([0-9a-zA-Z]+)[_]([0-9a-zA-Z]+).*$ results.asp?kwds=$1+$2+$3
RewriteEngine on
RewriteRule ^([0-9a-zA-Z]+)[_]([0-9a-zA-Z]+).*$ results.asp?kwds=$1+$2


The problem is, if for example i have a real file that is named second_test.html on my server with  content in it.  

When i type in www.mysite.com/second_test.html  ....it rightfully takes me to

www.mysite.com/result.asp?kwds=second test

Is there a way for me to exempt certain files from being excuted by this script?
So that when i type in www.mysite.com/second_test.html i get the actual file on the server not the generated page. I only want to be able to specify certain files not to be excuted by this script.

 Please help.

Thanks.
mrperfect75000Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

HackneyCabCommented:
How about a RewriteCond that checks for a set of document names:

RewriteCond %REQUEST_URI !^/path/(doc-name1|doc-name2|doc-name3)\.html$

The next RewriteRule that follows this can only execute if the REQUEST_URI is none of the following:

/path/doc-name1.html
/path/doc-name2.html
/path/doc-name3.html

You ought to be able to use this pattern to rule out a number of actual static HTML files that you don't want redirected to a script.

However, my site is small, and I'm not sure if this multi-name pattern can scale up to a large number of document names.

See the RewriteCond section in the Apache docs:

http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html#RewriteCond
shatteredstoneCommented:
If you want a generic rule that only rewrites if a given file does NOT exist (and it sounds like you might want that),

RewriteCond %{REQUEST_FILENAME} !-f

would test for that (inside the server configuration; insite a .htaccess it's %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} instead).

You also do not need more than one RewriteEngine on. So in the end, your ruleset would look like this :

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([0-9a-zA-Z]+)[_]([0-9a-zA-Z]+)[_]([0-9a-zA-Z]+).*$ results.asp?kwds=$1+$2+$3
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([0-9a-zA-Z]+)[_]([0-9a-zA-Z]+).*$ results.asp?kwds=$1+$2
HackneyCabCommented:
Actually, shatteredstone's solution does seem like a good one, and I'm guessing that it would scale better than mine.

Just make sure that, as with any user-supplied values, you check carefully the values provided as the query string to results.asp.
mrperfect75000Author Commented:
Thanks! If i had more than one file i want to exlude - do i have to do include each one?

Thanks for your help...
shatteredstoneCommented:
If you go with HackneyCab's way, you can use more than one RewriteCond, yes. i.e.

RewriteCond %{REQUEST_URI} !^/path/(doc-name1|doc-name2|doc-name3)\.html$
RewriteRule ^([0-9a-zA-Z]+)[_]([0-9a-zA-Z]+)[_]([0-9a-zA-Z]+).*$ results.asp?kwds=$1+$2+$3

will not rewrite the 3 files /path/doc-name1.html, /path/doc-name2.html, and /path/doc-name3.html (since it's just a regular regex). If one regex is not enough, you could get the same result with

RewriteCond %{REQUEST_URI} !^/path/doc-name1\.html$
RewriteCond %{REQUEST_URI} !^/path/doc-name2\.html$
RewriteCond %{REQUEST_URI} !^/path/doc-name3\.html$
RewriteRule ^([0-9a-zA-Z]+)[_]([0-9a-zA-Z]+)[_]([0-9a-zA-Z]+).*$ results.asp?kwds=$1+$2+$3

(you can have as many RewriteConds as you want on a single RewriteRule; they are then logically ANDed together, unless you use an [OR] flag, but that is not applicable here).

If you go with my solution, it will automatically not rewrite any URL for which a file exists, so no URLs to not be rewritten need to be explicitly included in your ruleset.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Apache Web Server

From novice to tech pro — start learning today.