Nginx的重写中替换字符?(Nginx Replace Characters in rewrite

2019-09-19 22:40发布

假设你有一个网址www.answers.mydomain.com/category/hello_world-123.html,但你要重写的路径部分为/分类/您好世界

你会如何去与Nginx上重写? 基本上我怎么可以格式化$ 1变量?

 

     server{
       listen 80;
       server_name  ~^(?<subdomain>.+)\.bg\.com$
       root /home/dan/Projects/rewrite-example;


       set $PREFERRED_DOMAIN $scheme://www.bg.com;

       if ($subdomain ~* answers) {
         rewrite ^(.*)$ $PREFERRED_DOMAIN/questions$1 permanent;
       }
     } 

 

Answer 1:

 server{
     listen 80;
     server_name  answers.bg.com;

     rewrite ^(.+/[a-z]+)-\d+\.html$ http://www.bg.com$1 permanent;
     rewrite ^(.+/[a-z]+)_([a-z]+)-\d+\.html$ http://www.bg.com$1-$2 permanent;
     rewrite ^(.+/[a-z]+)_([a-z]+)_([a-z]+)-\d+\.html$
             http://www.bg.com$1-$2-$3 permanent;
     rewrite ^(.+/[a-z]+)_([a-z]+)_([a-z]+)_([a-z]+)-\d+\.html$
             http://www.bg.com$1-$2-$3-$4 permanent;
     rewrite ^(.+/[a-z]+)_([a-z]+)_([a-z]+)_([a-z]+)_([a-z]+)-\d+\.html$
             http://www.bg.com$1-$2-$3-$4-$5 permanent;
 }
  • http://nginx.org/en/docs/faq/variables_in_config.html
  • http://wiki.nginx.org/IfIsEvil
  • http://wiki.nginx.org/Pitfalls
  • http://nginx.org/en/docs/http/converting_rewrite_rules.html


文章来源: Nginx Replace Characters in rewrite?
标签: nginx rewrite