黑莓的 .htaccess 重定向不起作用

发布于 2024-09-24 13:26:25 字数 383 浏览 0 评论 0原文

我试图通过执行以下操作将黑莓用户重定向到我的移动网站:

# redirect for blackberry users
RewriteCond %{HTTP_USER_AGENT} ^.BlackBerry.$
RewriteRule ^(.*)$ http://www.domain.com/m/ [R=301]

在我的 .htaccess 中,但是当我尝试从设备访问时没有任何反应,我已经删除了缓存和 cookie,并且没有任何效果。我一直在谷歌上搜索,似乎我正确地进行了重定向,但我想不是,我错过了什么?

顺便说一句,我的 .htaccess 仅包含该内容。

编辑 我的服务器根目录中的 .htaccess 。

I am trying to redirect blackberry users to my mobile site by doing:

# redirect for blackberry users
RewriteCond %{HTTP_USER_AGENT} ^.BlackBerry.$
RewriteRule ^(.*)$ http://www.domain.com/m/ [R=301]

in my .htaccess, but nothing happens when I try to access from the device, I've already deleted cache and cookis and nothing works. I have been googling around and it seems I'm doing the redirect correctly but I guess not, what am I missing?

My .htaccess only contains that by the way.

Edit
The .htaccess in my server's root.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

謌踐踏愛綪 2024-10-01 13:26:25

如果这不是 .htaccess 文件中的唯一规则,则可能会遇到后面的规则弄乱您的重定向的问题。要立即重定向,您需要包含 L 标志。

我还怀疑您的用户代理正则表达式对于您正在测试的输入可能不正确,因为两个 . 仅匹配单词“BlackBerry”两侧的一个字符。通过检查您是否已经位于 /m/ 中来防止重定向循环也是一个好主意(尽管如果您在该目录的 .htaccess 文件中有 mod_rewrite 指令,那么它就是不重要)。

将所有这些放在一起,我们得到如下内容:

# Check for x-wap-profile/Profile headers
RewriteCond %{HTTP:x-wap-profile} !^$ [OR]
RewriteCond %{HTTP:Profile}       !^$ [OR]
# Check for BlackBerry anywhere in the user agent string
RewriteCond %{HTTP_USER_AGENT}    BlackBerry [NC]
# Make sure we're not in /m/ already
RewriteCond %{REQUEST_URI}        !^/m/
RewriteRule ^ http://example.com/m/ [R=301,L]

您可能还希望 RewriteRule 为...

RewriteRule ^.*$ http://example.com/m/$0 [R=301,L]

...如果内容在 < 中命名相同(但适合移动设备)。 code>/m/ 目录。

If this isn't the only rule in your .htaccess file, you might have an issue where a later rule messes up your redirect. To redirect immediately, you need to include the L flag.

I also suspect that your regular expression for the user agent is probably not correct for the input you're testing against, since the two . match just one character on either side of the word "BlackBerry". It would also be a good idea to guard against a redirect loop with a check to see if you're already in /m/ (although if you have mod_rewrite directives in a .htaccess file in that directory it's not important).

Putting all of that together, we get something like the following:

# Check for x-wap-profile/Profile headers
RewriteCond %{HTTP:x-wap-profile} !^$ [OR]
RewriteCond %{HTTP:Profile}       !^$ [OR]
# Check for BlackBerry anywhere in the user agent string
RewriteCond %{HTTP_USER_AGENT}    BlackBerry [NC]
# Make sure we're not in /m/ already
RewriteCond %{REQUEST_URI}        !^/m/
RewriteRule ^ http://example.com/m/ [R=301,L]

You may also want that RewriteRule to be...

RewriteRule ^.*$ http://example.com/m/$0 [R=301,L]

...if the content is named the same (but mobile-friendly) in the /m/ directory.

绮筵 2024-10-01 13:26:25

确保您的 BlackBerry 未处于“模拟模式”,在该模式下,它会传递 IE 或 Firefox 而不是 BlackBerry 的用户代理。您可以在浏览器的选项屏幕中查看。

解决此问题的更好方法是将重写规则基于“x-wap-profile”和/或“profile”标头,移动浏览器应始终准确发送这些标头。

Make sure your BlackBerry is not in "emulation mode" where it passes the user-agent for IE or Firefox instead of BlackBerry. You can check in the browser's option screen.

A better way around this would be to base your rewrite rule on the "x-wap-profile" and/or "profile" headers, which the mobile browser should always send accurately.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文