简体   繁体   中英

htaccess redirect all asset URLs except starting with specific strings

Let's say the project structure is like this:

/
/public/assets/css/<css files go here>
/public/assets/js/<js files go here>
/public/assets/img/<image files go here>
/storage/assets/css/<css files go here>
/storage/assets/js/<js files go here>
/storage/assets/img/<image files go here>
/resources/assets/css/<css files go here>
/resources/assets/js/<js files go here>
/resources/assets/img/<image files go here>

I would like to redirect all css and js asset requests to /public/ except the requests which start with resources or storage

for example this request: /assets/css/style.css has to be redirected to: /public/assets/css/style.cc

But, /storage/assets/css/style.cc has to stay as it is.

What I am trying is something like this:

RewriteRule ^![storage|resources](.*\.css)$ /public/$1  [L,R=302]
RewriteRule ^![storage|resources](.*\.js)$ /public/$1  [L,R=302]

I know I am missing something in regex.

Does anyone know how to correct it?

Thank you in advance

You are on the right track, very close to a solution but you have to apply your condition inside a RewriteCond :

RewriteCond %{REQUEST_URI} !^/?(storage|resources|public)
RewriteRule .*\.(css|js)$ /public/$0  [L,R=302]

Otherwise you can go with a one-liner RewriteRule which uses a negative lookahead to apply redirection rule:

RewriteRule ^(?!storage|resources|public).*\.(css|js)$ /public/$0  [L,R=302]

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