简体   繁体   中英

Nginx path folder rewrite

Simple path rewrite in Nginx, I can't get it to work.

from this:

/assets/sites/fpi/css/reset.css

to this:

/assets/sites/default/css/reset.css

my rule looks like this:

rewrite ^/assets/sites(.+)$ /assets/sites/default/$1 last;

/** UPDATE **/

Thanks for the helpful answers so far. I would like a more generic way to target the "fpi" section of the URL path.

Something like:

<pre>
rewrite ^(/assets/sites)/\w*/(.*)$ $1/default/$2 last;
</pre>

Which would match any part of that section so all urls matching this pattern:

<pre>
/assets/sites/fpi/css/reset.css
/assets/sites/gbc/css/reset.css
/assets/sites/ekg/css/reset.css
</pre>

would shift to:

<pre>
/assets/sites/default/css/reset.css
/assets/sites/default/css/reset.css
/assets/sites/default/css/reset.css
</pre>

Thanks

Instead of last , use break , redirect or permanent . The differences are subtle:

  • break will stop processing rewrite rules for the current location block, which means that you will get out of your rewrite infinite loop. It will however cause any subsequent rewrite rules to be ignored if the break -containing rule was triggered. Unlike last , no other rewrite rules will be processed from there on and the rule cannot be re-evaluated by itself .
  • permanent allows you to do an immediate 301 HTTP redirect (ie it's at this address for good! ). This is particularly good for search engines and browser caches to allow caching and less resource distribution (= less wasted bandwidth on your side)
  • redirect is the same as permanent , but with a 302 instead (I moved temporarily). Less good.

I'd advocate permanent , personally. Give your visitors a chance to cache the CSS stylesheet.

Your regex does not match your specification. Use this:

rewrite ^(/assets/sites)/fpi/(.*)$ $1/default/$2 last;

Your regex in the example takes the fpi with it.

From the documentation:

rewrite last - completes processing of current rewrite directives and restarts the process (including rewriting) with a search for a match on the URI from all available locations.

Your rewritten URL will match the pattern that is being matched so it will continually rewrite it.

You should change the matcher (or the rewritten path) to not match it again.

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