Nginx的替换字符重写? [英] Nginx Replace Characters in rewrite?

查看:719
本文介绍了Nginx的替换字符重写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您有一个网址www.answers.mydomain.com/category/hello_world-123.html,但您想将路径部分重写为/category/hello-world

Say you have a URL www.answers.mydomain.com/category/hello_world-123.html but you want to rewrite the path part as /category/hello-world

如何通过nginx重写来解决这个问题?基本上,我该如何格式化$ 1变量?

How would you go about that with an nginx rewrite? Basically how can I format the $1 variable?


 

     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;
       }
     } 

 

推荐答案

 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;
 }

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆