使用 mod_rewrite 重写用户代理规则 [英] Rewrite rule for user agent with mod_rewrite

查看:26
本文介绍了使用 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]

但是我在启动 Web 服务器时得到的只是:

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?

谢谢,J

推荐答案

有 2 个错误:

首先:

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\/(.*)$

第二:

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

302 是重定向 HTTP 状态代码,但您没有指定要重定向.

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]

这篇关于使用 mod_rewrite 重写用户代理规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆