I am trying to redirecto to other machine by filtering the user agent in a apache web server. I have a web server in the machine with the ip, but this web server only should send the page to the requests WITHOUT this user agent:

Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:99.0) Gecko/20

Instead it should redirect the request to that have other web server running.

I tried to modify the file /etc/apache2/apache2.conf with the next code

RewriteEngine on
RewriteCond ${HTTP_USER_AGENT} "!=Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:99.0) Gecko/20"
RewriteRule ^(.*)$ [L,R=302]

But doesn't work, it always show index.html from the machine (it doesnt redirect). Why? How can I do that?


I think I'm indicating wrong the user_agent because this works fine:

RewriteEngine on
RewriteCond ${HTTP_USER_AGENT} Ubuntu
RewriteRule ^(.*)$ - [R=403,L]

So the question is, how can I specify a particular user_agent? and how can I spacify all user_agent exept that particular user_agent?

  • You should add a backreference in the rewrite rule like this: RewriteRule ^(.*)$$1 [L,R=302] This may not solve the entire issue as the redirect is still not working!, but this is required. May 10, 2022 at 13:23
  • It still always show the main page, but thanks man!
    – P00
    May 10, 2022 at 13:28
  • You have to put these few lines in your virtual host config. For instance in the /etc/apache2/sites-enabled/000-default.conf, not in the main config file. This config worked for me. Beware of the default behaviour of your browser which may switch to https. May 10, 2022 at 13:51
  • I have in the 000-default.conf the lines to redirect http to https (and those work fine), but I've tried to put both fragments code in the question in the 000-default and in the desfault-ssl and it doesn't work
    – P00
    May 10, 2022 at 14:02

1 Answer 1


As per the documentation of the RewriteCond, you can use either regular expressions or plain string

... CondPattern is usually a perl compatible regular expression, but there is additional syntax available to perform other useful tests against the Teststring: ...

So, in order to rewrite for any user agent except this one "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:99.0) Gecko/20", you have to use the following:

RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} "!=Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:99.0) Gecko/20"
RewriteRule ^/(.*)$$1 [L,R=302]

You can also combine multiple RewriteCond for the same RewriteRule. But this is not what you are looking for.

  • Note the % sign instead of $ in %{HTTP_USER_AGENT} May 10, 2022 at 14:38
  • But if I what not just to redirect the ubuntu requests, but also that have this specific user agent "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:99.0) Gecko/20", how can I do it? I mean, If in the command you told me replace *buntu with "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:99.0) Gecko/20" it doesn't work
    – P00
    May 10, 2022 at 14:43
  • Ok, I've got it, thacks!!
    – P00
    May 10, 2022 at 15:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .