如何将下划线替换为Nginx [英] How to replace underscore to dash with Nginx

查看:77
本文介绍了如何将下划线替换为Nginx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次使用Nginx,对此一无所知.

I'm using Nginx for the first time ever, and got basically no knowledge of it.

我需要在100多个网址中将"_"替换为-".我认为使用Nginx一定有一种简单的方法,但是在Google上找不到任何东西.

I need to replace "_" with "-" in 100+ URL. I figured there must be an easy way to do this with Nginx, but can't find anything on Google.

谢谢!

我的网址例如: http://www.mywebsite.com/this_category/page1. php

我需要使它成为: http://www.mywebsite.com/this- category/page1.php

推荐答案

从2013-04年至2015年,此问题的现有答案都不够理想和丑陋-一个依赖过多的复制粘贴,并且不清楚的错误处理/报告功能,另外一个涉及未定义数量的不必要的301 Moved交互,供客户端处理.

Both existing answers to this question from 2013-04 and 2015 are rather suboptimal and ugly — one relies on too much copy-paste and has unclear error handling/reporting, and another one involves having an undefined number of unnecessary 301 Moved interactions for the client to handle.

有更好的方法,隐藏在2013年的质量检查对中 -02-在2013-04年这个问题之前只有几个月!它涉及到 http://nginx.org/r/rewrite last参数.指令,如果与last匹配的结果会导致nginx停止处理重写指令,然后根据修改后的$uri返回以搜索适当的新" location,从而导致内部重定向循环nginx最多10次(例如,根据 http://nginx.org/r/internal进行10次内部重定向) ),如果您超过10个周期的限制,则记录500 Internal Server Error.

There's a better way, hidden in plain sight at a QA pair from 2013-02 — only a couple of months prior to this very question from 2013-04! It involves relying on the last parameter for the http://nginx.org/r/rewrite directive, which will cause nginx to stop processing the rewrite directives should one with last result in a match, and go back in search of an appropriate "new" location per the modified $uri, causing an internal redirect loop within nginx for up to 10 times (e.g., 10 internal redirects, as per http://nginx.org/r/internal), recording a 500 Internal Server Error if you exceed the limit of 10 cycles.

从某种意义上说,这个答案与原始答案很相似,只是您免费获得10的额外系数,从而减少了对复制粘贴的要求.

In a sense, this answer is similar to the original one, it's just that you get an extra factor of 10 for free, resulting in fewer copy-paste requirements.

# Replace maximum of 3 or 1 underscores per internal redirect,
# produce 500 Internal Server Error after 10 internal redirects, 
# supporting at least 28 underscores (9*3 + 1*1) and at most 30 (10*3).
location ~ _ {
    rewrite "^([^_]*)_([^_]*)_([^_]*)_(.*)$" $1-$2-$3-$4 last;
    rewrite "^([^_]*)_(.+)$" $1-$2 last;
    return 301 $uri;
}

这篇关于如何将下划线替换为Nginx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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