Home > Enterprise >  NGINX regexp or statement for redirect
NGINX regexp or statement for redirect

Time:11-23

I currently have the following NGINX config to redirect all .html requests to the non .html prefix URI if "static" is not in the request URI:

if ($request_uri ~ ^/(?!static/)(.*)\.html) {
    return 301 /$1$is_args$args;
}

However, I also want to avoid redirecting to the non .html variant if the request contains "googled" in the URI for the tag manager verification.

I've tried adding OR statements in this expression, but I can't seem to figure it out because I lack the knowledge to do something like that in regexp.

How would I add an OR statement in this case?

CodePudding user response:

If I understand you correctly you don't want to match for example /googled123.html.

You can do:

^\/(?!static\/)(?!.*googled)(.*)\.html

Regex demo.


^ - beginning of string

(?!static\/) - don't match string if begins with static/

(?!.*googled) - don't match string if conains googled

(.*)\.html - match all without .html at the end.

  • Related