bear
bear

Reputation: 11615

don't rewrite static css/js/img files

I'm trying to get my htaccess file not to rewrite my static files (js/css/images).

This is my current htaccess file:

RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule    !\.(jpg|css|js|gif|png)$    public/    [L]
RewriteRule !\.(jpg|css|js|gif|png)$ public/index.php?url=$1

How do I rewrite it?

Upvotes: 6

Views: 15301

Answers (3)

Born-kes
Born-kes

Reputation: 21

for posterity.

Maybe my rules will be useful

\www\.htaccess

#file: \www\.htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} \.(css|js|html?|png|gif|jpg|jpe?g|svg|xls.?|pdf|docx?|eot|ttf|woff2?)$ [NC]
RewriteRule (.*\.[css|js|html?|png|gif|jpg|jpe?g|svg|xls.?|pdf|docx?|eot|ttf|woff2?]+) public/$1?a [QSA,L]

RewriteCond %{REQUEST_URI} \.(css|js|html?|png|gif|jpg|jpe?g|svg|xls.?|pdf|docx?|eot|ttf|woff2?) [NC]
RewriteRule (.*\.[css|js|html?|png|gif|jpg|jpe?g|svg|xls.?|pdf|docx?|eot|ttf|woff2?]+) public/$1 [QSA,L]

#RUN TEST
RewriteCond %{REQUEST_URI} !\.(css|js|html?|png|gif|jpe?g|svg|xls.?|pdf|docx?|eot|ttf|woff2?)$ [NC]
RewriteRule ^(.*)$ index.php?REQUEST_FILENAME=%{REQUEST_FILENAME}&date=$1&REQUEST_URI=%{REQUEST_URI} [QSA,L]
#END TEST

RewriteRule . index.php?url=%{REQUEST_URI} [QSA,L]

ErrorDocument 404 index.php?error=404

\www\index.php

<?php
// file: /www/index.php
 var_dump($_GET);

\www\app\.htaccess

#file: \www\app\.htaccess`

 RewriteEngine On
 RewriteRule ^(.*)$ ../_%{REQUEST_URI} [QSA,L]

\www\public\.htaccess

#file: \www\public\.htacces`
 RewriteEngine on

 RewriteCond %{REQUEST_FILENAME} !-l 
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteCond %{REQUEST_FILENAME} !-f

 RewriteRule ^(.*)$ index.php?url=%{REQUEST_URI}&rewrite=$1 [QSA,L]

/www/public/index.php

<?php
// file: /www/public/index.php
 var_dump($_GET, __DIR__);

Upvotes: 2

David
David

Reputation: 104

This only works for the one line below it. So if you have many RewriteRules it could help to use something like this:

    RewriteEngine on

    RewriteRule ^(.*?)\.(php|css|js|jpg|jpeg|png|pdf)$ - [L]
    RewriteRule ^(.+)/(.+)/?$ index.php?page=$1&subpage=$2 [L]
    RewriteRule ^(.+)$ index.php?page=$1 [L]

Upvotes: 6

Kamil Šrot
Kamil Šrot

Reputation: 2221

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^.*\.(jpg|css|js|gif|png)$ [NC]
RewriteRule ^(.*)$ public/index.php?url=$1

All requests to not existing files which doesn't end with listed extensions (case nonsensitive match) are rewritten to public/index.php passing the current URL as url= GET argument

Upvotes: 16

Related Questions