Rewrite rule for user agent with mod_rewrite

I am trying to redirect all requests to a domain from a specific user agent to a subdomain. My rule is this:

RewriteEngine on RewriteCond %{HTTP_USER_AGENT} ^Test Agent/(.*)$ // <-- Line 4 RewriteRule ^(.*)$ https://test.domain.com/$1 [L,302] 

But all I get when starting a web server is:

 Syntax error on line 4 of /var/www/misafe/internal/misafe-old.conf: RewriteCond: bad flag delimiters 

This looks good to me, but I obviously missed something, and the error doesn't help much. Any ideas?

Thanks J

+7
source share
2 answers

There are 2 errors:

Firstly:

 RewriteCond %{HTTP_USER_AGENT} ^Test Agent/(.*)$ 

You need to avoid the space and slash in the regex pattern.

 RewriteCond %{HTTP_USER_AGENT} ^Test\ Agent\/(.*)$ 

Secondly:

 RewriteRule ^(.*)$ https://test.domain.com/$1 [L,302] 

302 is the HTTP redirect status code, but you did not indicate that you are redirecting.

 RewriteRule ^(.*)$ https://test.domain.com/$1 [L,R=302] 
+17
source

line: RewriteRule ^(.*)$ https://test.domain.com/ $1 [L,302]

shuld be: RewriteRule ^(.*)$ https://test.domain.com/ $1 [R=302]

+1
source

Source: https://habr.com/ru/post/1334681/


All Articles