Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
316 views
in Technique[技术] by (71.8m points)

mod rewrite - Regex match this OR that?

What is the regex for this?

Match if string

  • NOT ( ends in .php OR ends in .html OR contains / )

Thank you!

Edit: I need the NOT part because the expression is to be used on an Apache mod rewrite, since I can't change the logic of mod_rewrite to avoid the NOT.

Edit: My initial effort was ([^/]+)(.html)$(^.php)$ - which is monstrously wrong

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Apache mod_rewrite does support negation, according to this document.

In mod_rewrite, the NOT character ('!') is also available as a possible pattern prefix. This enables you to negate a pattern; to say, for instance: ``if the current URL does NOT match this pattern''. This can be used for exceptional cases, where it is easier to match the negative pattern, or as a last default rule.

So you should be able to do something like:

!^(.*?/.*|.*?.(?:php|html)$)$

And that should match anything which does not contain / and does not end in .php or .html


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...