Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rewrite rule for user agent with mod_rewrite

I'm trying to redirect all requests to a domain from a particular user-agent to a subdomain. My rule is as follows:

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

But all I get when starting the web server is:

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

It looks OK to me but I'm obviously missing something and the error is not helping much. Any ideas?

Thanks, J

like image 787
JWood Avatar asked Jan 07 '11 16:01

JWood


2 Answers

There are 2 bugs:

First:

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

You need to escape the space and forward slash in your regular expression pattern.

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

Second:

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

302 is a redirect HTTP status code, but you didn't specify that you are redirecting.

RewriteRule ^(.*)$         https://test.domain.com/$1          [L,R=302]
like image 102
Jonathan Amend Avatar answered Nov 15 '22 19:11

Jonathan Amend


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

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

like image 24
virhilo Avatar answered Nov 15 '22 21:11

virhilo