Edward Tanguay
Edward Tanguay

Reputation: 193322

How to force .htaccess used for routing to not route .css, .js, .jpg, etc. files?

I have the following .htaccess file in a subdirectory of a site which allows me to route all URLs to index.php where I can parse them.

However, it's not allowing the standard files that I need for the website, e.g. css, javascript, pngs, etc.

What do I need to change (I assume in the fourth line) to allow these files so they don't get routed to index.php?

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|public|css|js|png|jpg|gif|robots\.txt)
RewriteRule ^(.*)$ index.php/params=$1 [L,QSA]
ErrorDocument 404 /index.php

Upvotes: 13

Views: 19913

Answers (4)

AbsoluteƵERØ
AbsoluteƵERØ

Reputation: 7880

Something I noticed. You're using the forward slash instead of a question mark... the params redirect would normally look like this:

RewriteRule ^(.*)$ index.php?params=$1 [L,QSA]

This should work by itself since any of those files *should* be real files.

ErrorDocument 404 /index.php    

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?params=$1 [L,QSA]

To have the site ignore specific extensions you can add a condition to ignore case and only check the end of the filenames in the request:

RewriteEngine On

RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?params=$1 [L,QSA]

If you're trying to ignore a folder then you could add:

RewriteEngine On

RewriteCond %{REQUEST_URI} !(public|css)
RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?params=$1 [L,QSA]

Upvotes: 13

goodeye
goodeye

Reputation: 2449

You have the right idea, tweaking the fourth line. The ^ is saying the various matching strings must be at the beginning of the line. If you don't care where any of these appear in the file, you can just remove the ^. That will avoid rewriting *.css, *.js, etc.; but will also not rewrite publicideas.html.

If you want to limit to just the suffixes, try this:

RewriteCond $1 !^(index\.php|public|.*\.css|.*\.js|.*\.png|.*\.jpg|.*\.gif|robots\.txt)$

This says to match anything at the beginning, then a ., then the suffix. The $ says match these at the end (nothing following).

I'm not sure about the public, so I left it (which means exactly public, with nothing else - probably not what you meant, but you can add a * before or after, or both).

Upvotes: 1

Denis de Bernardy
Denis de Bernardy

Reputation: 78483

The easiest is to ignore them explicitly early in your rules:

RewriteRule \.(css|js|png|jpg|gif)$ - [L]
RewriteRule ^(index\.php|robots\.txt)$ - [L]

This avoid carrying them around all over the place with RewriteCond.

At your option, check that the file exists prior to doing so:

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule \.(css|js|png|jpg|gif)$ - [L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(index\.php|robots\.txt)$ - [L]

(Note that the file check generates an extra disk access.)

Upvotes: 7

feeela
feeela

Reputation: 29932

RewriteCond %{REQUEST_FILENAME} !-f alone should be enough.

Another option is t exclude specific files from the rewrite. From the TYPO3 packages:

# Stop rewrite processing, if we are in the typo3/ directory.
# For httpd.conf, use this line instead of the next one:
# RewriteRule ^/TYPO3root/(typo3/|t3lib/|fileadmin/|typo3conf/|typo3temp/|uploads/|favicon\.ico) - [L]
RewriteRule ^(typo3/|t3lib/|fileadmin/|typo3conf/|typo3temp/|uploads/|favicon\.ico) - [L]

This rule should appear before your actual rewrite. It should be

RewriteRule ^(public/|*\.css|*\.js|*\.png|*\.jpg|*\.gif|robots\.txt) - [L]

in your case.

Upvotes: 0

Related Questions