bnahin
bnahin

Reputation: 816

.htaccess rewrite everything except images, scripts, and stylesheets

I have a web project with two directories: "core" and "public".

"Core" contains all of the controllers, views, and files required for the Model-View-Controller.

"Public" contains all public files, like js, css, and less, that can be accessed directly.

I have the following .htaccess in the main directory:

    <IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_URI}  !(\.png|\.jpg|\.gif|\.jpeg|\.bmp|\.css|\.js|\.less|\.coffee)$
    RewriteRule    ^$    core/    [L]
    RewriteRule    (.*) core/$1    [L]
 </IfModule>

However, it still rewrites those files to the 'core' directory.

What am I doing wrong?

Upvotes: 1

Views: 182

Answers (1)

Mike Rock&#233;tt
Mike Rock&#233;tt

Reputation: 9007

I recommend you use this instead:

RewriteEngine on

# First, check if a specific type is being requested
RewriteCond %{REQUEST_URI} !\.(png|jpg|gif|jpeg|bmp|css|js|less|coffee)$ [NC]
# Second, check if the request is for an existing file
RewriteCond %{REQUEST_FILENAME} -f
# If both conditions are true, then skip rewriting
RewriteRule ^ - [L]

# Otherwise, continue:
RewriteRule ^$   core/   [L]
RewriteRule (.+) core/$1 [L]

The reason your assets were being sent to core is because the conditions only work for the first rule, which was for your application index. Using this method tells mod_rewrite to skip rewriting if an asset is being requested. Once it does that, it can continue with all other rules.

Upvotes: 1

Related Questions