Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

.htaccess

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
like image 857
Edward Tanguay Avatar asked Jun 10 '13 15:06

Edward Tanguay


People also ask

What is RewriteRule in htaccess?

htaccess rewrite rules can be used to direct requests for one subdirectory to a different location, such as an alternative subdirectory or even the domain root. In this example, requests to http://mydomain.com/folder1/ will be automatically redirected to http://mydomain.com/folder2/.

How do I know if htaccess is working?

In your browser, open /test , with the correct domain name. So, it should look like http://localhost/test or http://example.org/test . If you see the following, it works!


3 Answers

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]
like image 121
AbsoluteƵERØ Avatar answered Nov 14 '22 09:11

AbsoluteƵERØ


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

like image 7
Denis de Bernardy Avatar answered Nov 14 '22 10:11

Denis de Bernardy


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

like image 1
goodeye Avatar answered Nov 14 '22 10:11

goodeye