Forum Moderators: phranque

Message Too Old, No Replies

.htaccess redirect all but images

         

wfernley

12:51 pm on Sep 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey everyone,

I created a new website that basically runs all from one page. Why? It's a long story ;)

Anyways, I have a .htaccess file that right now runs fine forwarding every request to one page. It looks like this:

RewriteRule (.*) pages\.php?page=$1

Now it works fine for what I have needed but the problem is, I have files and images in specific directories that need to be pulled in the pages and the .htaccess file just goes to pages.php instead of just accessing the file or image.

How can I change that RewriteRule to only work on web pages and not images or other file downloads (ex. pdf, doc, xls, mp3).

Please note, the following will need to forward to pages.php:

/aboutus.html
/products/product.html
/help/contactus.html

The following cannot forward to pages.php:
/images/logo.gif
/files/document.pdf

An easy solution might even be telling the .htaccess file not work on files within the /images/ and /files/ directories.

Can anyone help? :)

Thanks in advance for your help!

Wes

jdMorgan

1:07 pm on Sep 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need to add an exclusion to the rule -- a conditional clause that determines whether the rule will be applied or not. This is the purpose of the RewriteCond directive.

So, with the specific filetypes you mentioned, we can say, rewrite only if NOT those filetypes:


RewriteCond %{REQUEST_URI} !\.(gif¦jpe?g¦png¦css¦js¦doc¦mp3¦pdf¦xls)$

This says, "Execute the subsequent rule only if the requested URI does NOT end with a string matching one of these patterns."

If your site is very busy, put the file extensions in order from most-frequently-requested to least. You want the rule to 'quit' as soon as possible for frequently-requested objects like gif images.

Replace the broken pipe characters "¦" above with solid pipe characters before use; Posting on this forum modifies the pipe characters.

For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Jim