6

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?

1
  • Why would your .htaccess will rewrite your static file if you don't want to rewrite it? Just clear your .htaccess and it wouldn't be rewritten. Also you said in your question not to rewrite and How do I rewrite it. Can you please explain a little bit what you are trying to do? Commented Nov 17, 2012 at 23:36

3 Answers 3

16
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

Sign up to request clarification or add additional context in comments.

Comments

6

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]

1 Comment

Thanks, Worked for me. Also it's better to add eot, ttf, woff, woff2 and svg to this list
2

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__);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.