search

Home  >  Q&A  >  body text

Regular expression - nginx regular expression, how to match not ending with / and not ending with .xml .html .htm

need

Requires a regular expression, used in nginx.
If $uri does not end with / and does not end with .xml, .html, or .htm, it will be permanent to $uri/.

location @rewrite {
    rewrite [^\/]$ $uri/ permanent;
    rewrite . /index.php?s=$uri&$args last;
}

try

My own writing method, but failed:

rewrite ([\/]|.xml|.html?)$ $uri/ permanent;

Question

1)如何才可以实现不以.xml .html /结尾的才重写的正则。
2)如何才可以在正则表达式[]中排除完整的一段单词?

Replenish

I used this sentence, and it seems that no problem has been found for the time being.

rewrite [^\/|\.xml|\.html?]$ $uri/ permanent;

Question, isn’t a-zA-Z0-9 in the square brackets []? Can you also write a complete matching string?

过去多啦不再A梦过去多啦不再A梦2776 days ago909

reply all(2)I'll reply

  • PHP中文网

    PHP中文网2017-05-16 17:27:48

    What you need is a regular expression engine that supports union, intersection, and difference: http://nfabo.cn/p/?p=1280
    In nginx, you can only use negation to reverse look around: .*(?<!.xml|.html|.htm|/)$

    reply
    0
  • 巴扎黑

    巴扎黑2017-05-16 17:27:48

    You can rewrite these files ending in .html, .htm, and .xml to their original directories first,
    The rest is achieved by applying your rewrite rules.

    reply
    0
  • Cancelreply