从正则表达式重定向中排除目录 [英] Excluding directory from regex redirect

查看:209
本文介绍了从正则表达式重定向中排除目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望将所有带下划线的URL重定向到其破折号.

I wish to redirect all URLs with underscores to their dashed equivalent.

例如/nederland/amsterdam/car_rental变为/nederland/amsterdam/car-rental.为此,我使用此处描述的技术:如何将下划线替换为Nginx .所以我的位置块匹配到:

E.g. /nederland/amsterdam/car_rental becomes /nederland/amsterdam/car-rental. For this I'm using the technique described here: How to replace underscore to dash with Nginx. So my location block is matched to:

location ~ (_) 

但是我只想对/admin名称空间中的URL 执行此操作.为此,我尝试将正则表达式与否定查询结合使用:

But I only want to do this on URLs not in the /admin namespace. To accomplish this I tried combining the regex with a negative lookup: Regular expression to match a line that doesn't contain a word?. The location now matches with:

(?=^(?!\/admin))(?=([^_]*))

Rubular报告字符串/nederland/amsterdam/car_rental以匹配正则表达式,而/admin/stats_dashboard不匹配,就像我想要的那样.但是,当我将此规则应用于nginx配置时,该站点最终会进入重定向循环.有什么我忽略的东西吗?

Rubular reports the string /nederland/amsterdam/car_rental to match the regex, while /admin/stats_dashboard is not matched, just as I want it. However when I apply this rule to the nginx config, the site ends up in redirect loops. Is there anything I've overlooked?

更新:我实际上并不想重写/admin名称空间中的任何内容.下划线到破折号的重写只应在/admin名称空间中的所有URL 上进行.

UPDATE: I don't actually want to rewrite anything in the /admin namespace. The underscore-to-dash rewrite should only take place on all URLs not in the /admin namespace.

推荐答案

Nginx位置匹配顺序是这样的:使用正则表达式定义的位置将按照它们在配置文件中的出现顺序进行检查,并且对正则表达式的搜索会在第一个匹配项时终止.

The Nginx location matching order is such that locations defined using regular expressions are checked in the order of their appearance in the configuration file and the search of regular expressions terminates on the first match.

有了这些知识之后,我将在鞋中使用正则表达式"admin"简单地定义一个位置,在该位置上方是您从

With this knowledge, in your shoes, I will simply define one location using a regular expression for "admin" above that for the underscores you got from the Stack Overflow Answer you linked to.

location ~ (\badmin\b) {
    # Config to process urls containing "admin"
}
location ~ (_) {
    # Config to process urls containing "_"
}

任何带有admin的请求都将被第一个位置块处理,无论它是否带有下划线,因为匹配的位置块出现在下划线之前.

Any request with admin in it will be processed by the first location block no matter whether it has an underscore or not because the matching location block appears before that for the underscores.

作为cnst在我的演出后几天发布的另一个答案,指向我发布的位置匹配顺序的文档的链接也表明您还可以使用^~修饰符来匹配/admin文件夹并跳过下划线的位置块.

As another answer posted by cnst a couple of days after mine shows, the link to the documentation on the location matching order I posted also indicates that you may also use the ^~modifier to match the /admin folder and skip the location block for the underscores.

我个人倾向于不使用此修饰符,而倾向于将基于正则表达式的位置与带注释的注释一起使用,但这当然是一个选择.

I personally tend not to use this modifier and prefer to band regex based locations together with annotated comments but it is certainly an option.

但是,根据您的设置,您将需要小心,因为以"/admin"开头但更长的请求可能与修饰符匹配并导致意外的结果.

However, you will need to be careful, depending on your setup, as requests starting with "/admin", but longer, may be matching with the modifier and lead to unexpected results.

如前所述,我更喜欢基于regex的方法,因为在没有明确了解的情况下,没人会开始随意更改配置文件中的事物顺序.

As said, I prefer my regex based approach safe in the knowledge that no one will start to arbitrarily change the order of things in the config file without a clear understanding.

这篇关于从正则表达式重定向中排除目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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