Nginx Replace Characters in rewrite?

2019-05-30 00:40发布

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

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

 

标签: nginx rewrite
1条回答
不美不萌又怎样
2楼-- · 2019-05-30 01:10
 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;
 }
查看更多
登录 后发表回答