Rewrite rule for user agent with mod_rewrite

橙三吉。 提交于 2019-12-18 04:51:04

问题


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


回答1:


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]



回答2:


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

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



来源:https://stackoverflow.com/questions/4627945/rewrite-rule-for-user-agent-with-mod-rewrite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!