Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex in mod_rewrite to match URLs NOT ending with 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

like image 852
johnjohn Avatar asked Jul 27 '10 16:07

johnjohn


1 Answers

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

like image 94
Peter Boughton Avatar answered Sep 30 '22 05:09

Peter Boughton