Redirect all media requests(css,js and images) with htaccess

Asked

Viewed 318 times

3

I am building an MVC application and was having some problems with the file path css,js and images no html... I was given a solution to use the tag <base> to define the path that all html files would follow, e.g.: <base href="http://localhost/mvc/>, only that from then on I had some problems to install javascript plugins, since when I put, for example, href="#" that by clicking instead of doing no action, this link led me to the url that was set with the tag <base> that would be http://localhost/mvc/#, I would like to know how to redirect all "requests" of these files to a subfolder, e.g.:

<link rel="stylesheet" href="main.css">
<img src="images/img2.jpg" />

All requests would lead to a subfolder, for example, PUBLIC, then only need to define in html the folders inside that public...

HTACCESS file:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

1 answer

2

Tested something like this? I believe it solves your problem.

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{DOCUMENT_ROOT}/public/$0 -f
    RewriteRule ^.+\.(jpg|jpeg|gif|png|ico|css|js|swf)$ /public/$0 [L]
  • I haven’t tried it yet, but I would also like to preserve the htaccess structure that I posted before, will that do it? Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.