Nginx的替换字符重写? [英] Nginx Replace Characters in rewrite?
本文介绍了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;
}
- 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
- 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的替换字符重写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文