简体   繁体   中英

.htacces redirect assets to assets folder

I have my PHP site setup with a folder for each site (eg Login), which has an Index.php file and site-specfic assets. However, some of the assets, which are required by every single page, are stored in a 'Assets' folder located directly under the highest level (does that make sense?).

I have played around with .htaccess and got this code

RewriteEngine on
RewriteBase /
RewriteRule ^(.*)$ http://janberktold.com/Assets/$1 [R=301,L]

However, my problem is: It redirects

localhost/Login/test.css

to

localhost/Assets/Login/test.css

instead of

localhost/Assets/test.css

How do I get my server to redirect to the correct path?

Try this

RewriteEngine on
RewriteBase /

# Rewrite if the file does not exists
RewriteCond %{REQUEST_FILENAME} !-f

# Rewrite only if the URI does not starts with Assets
RewriteCond %{REQUEST_URI} !^/Assets

# Rewrite any assets file
RewriteRule ([^/]*).(css|js|png|jpe?g)$ Assets/$1.$2 [L]

This should rewrite any assets files localhost/dir/file.css or localhost/dir/dir2/file.css to localhost/Assets/file.css

Replace your rules with:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(?:[^/]*/)*([^/.]+\.(?:jpe?g|gif|bmp|png|tiff|css|js))$ /Assets/$1 [R=301,L,NC]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM