Fractaliste
Fractaliste

Reputation: 5957

Check for specific header value in RewriteCond

I'm configuring my new Apache2.4 web server.

Into one of my VirtualHost I configure a redirect for everybody to a maintenance page when I set the <If> condition to true, except if a custom request's header contains a value:

<If "true">
    RewriteCond %{HTTP:UserId} !=ab123
    RewriteCond %{REQUEST_URI} !=/maintenance.html
    RewriteRule ^ /maintenance.html [R=302]
</If>

Is it possible to allow the check of many values instead of only one?

I would like something like:

RewriteCond %{HTTP:UserId} not in (ab123, ze678, gt456)

Which are admin's UserId.

Upvotes: 1

Views: 9448

Answers (1)

arco444
arco444

Reputation: 22831

I don't think you need to include any IF condition. The below should be fine:

RewriteCond %{HTTP:UserId} !^(ab123|ze678|gt465)$
RewriteCond %{REQUEST_URI} !maintenance.html
RewriteRule ^ /maintenance.html [R=302]

The first two RewriteCond work in a logical AND so the rule won't be executed unless both are true. You can use an ORd regex to check the userids coming in your header.

And if the field contains other values, you can remove the anchors ^ and $, but then the check won't be as safe

Upvotes: 4

Related Questions