如何更换下划线与Nginx的冲(How to replace underscore to dash

2019-08-21 02:41发布

我使用的Nginx的第一次,并得到了基本上毫不知情。

我需要更换“_”,“ - ”,在100多个网址。 我想必须有一个简单的方法与Nginx的做到这一点,但找不到任何谷歌。

谢谢!

编辑:

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

我想这能成为: http://www.mywebsite.com/this-category/page1.php

Answer 1:

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.

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.

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


Answer 2:

没有,有没有一种简单的方法来做到这一点,但重写引擎还是能够强迫做,假设你可以把一个合理的上限就需要在一个单一的URL转换横线的数量(或者即使你不” T,一看便知结束。)

以下是我会做它(测试代码):

rewrite ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4-$5-$6-$7-$8-$9;
rewrite ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4-$5;
rewrite ^([^_]*)_([^_]*)_(.*)$ $1-$2-$3;
rewrite ^([^_]*)_(.*)$ $1-$2;

四个重写在URL中短划线分别平移第一8,4,2和1下划线。 每个规则下划线的人数会减少故意的2权力。 此块是最有效的一组规则将从0到15次下划线在单个URL翻译,即使用匹配或不匹配每个单独的规则的所有16种组合。

您还会注意到,我用[^_]*上除了最后一个,在每一个规则每个组。 这避免了具有正则表达式引擎在非匹配的情况下进行不必要的回溯。 基本上,有九项普遍星星.*的正则表达式导致“最坏情况”,这是一种非匹配,这实际上是你的最频繁的情况下为O(n 9)的复杂性(这是相当不错)。 (我可以推荐这本书对于那些谁愿意真正理解一个正则表达式实际上是由底层库执行。)

出于这个原因,如果你可以把一个小的限制上划线的数量超过15个,我会建议收走了第一条规则,或者是前两种。 仅在过去的三个规则将转化多达7个下划线; 最后两个将转化可达3。

最后,你没有提到将用户重定向到新的URL。 (而不是仅仅服务于在下划线的链接,并在正确的,这是在搜索引擎坚果通常皱起了眉头。仅供参考内容)。如果这是你需要什么,你将不得不把这些改写成特殊位置被触发的网址下划线的存在,并且将用户重定向到在四个重写结束的新网址:

location ~ _ {
  rewrite ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4-$5-$6-$7-$8-$9;
  rewrite ^([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ $1-$2-$3-$4-$5;
  rewrite ^([^_]*)_([^_]*)_(.*)$ $1-$2-$3;
  rewrite ^([^_]*)_(.*)$ $1-$2;
  rewrite ^ $uri permanent;
}

这也增加了traslating在一个网址下划线的数量不受限制,在多于一个重定向的牺牲用户的浏览器的好处。

HTH ;-P



Answer 3:

这都过期时间,但我必须说明,上面的答案需要更正为使用n个不同的数字rewirtes的,其中n是存在于URL下划线的量是完全unnecesary。 这个问题可以用3个不同的位置指示和重写规则,同时concidering在他们的正规表示在以下情况下得到解决:

  1. 有在URL的末尾一个或多个下划线。
  2. 有一个或多个下划线在URL的开始
  3. 有一个或多个undersocres在URL的中间

      location ~*^/(?<t1>\_+)(?<t2>[a-zA-Z0-9\-]*)$ { return 301 $scheme://$host/-$t2; } location ~*^/(?<t2>[a-zA-Z\_0-9\-]*)(?<t1>\_+)$ { return 301 $scheme://$host/$t2-; } location ~*^/(?<t2>[a-zA-Z0-9\-]*)(?<t1>\_+)(?<t3>[a-zA-Z0-9\-]*)$ { return 301 $scheme://$host/$t2-$t3; } 

这三个指令将递归以取代所有的下划线“ - ”,直到没有剩下

-BeWilled



文章来源: How to replace underscore to dash with Nginx