【问题标题】:Redirect mobile devices example.com to example.com/m with htaccess使用 htaccess 将移动设备 example.com 重定向到 example.com/m
【发布时间】:2020-07-19 06:38:43
【问题描述】:

我有 2 个独立的网站,1 个是大型设备(如笔记本电脑和个人电脑)的主站点。

https://example.com/

我的第二个网站适用于所有移动设备和平板电脑。

https://example.com/m

我发现了一个帖子about this here

所以我从以下编辑创建这个

RewriteCond %{REQUEST_URI} !^/m/.*$
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteRule ^(.*)$ /m/ [L,R=302]

但它并不能解决所有问题。它目前接收来自移动设备的所有请求并将它们定向到

https://example.com/m

如何从移动设备发出以下请求,

https://example.com/mywebpage1 => https://example.com/m/mywebpage1

【问题讨论】:

  • 你试过这个吗:stackoverflow.com/questions/3680463/…
  • 是的,我在创建新帖子之前看过这篇文章,以下建议均不涉及重定向到具有以下 url 工作的子目录。该线程大约在 10 年前得到支持,我拥有的这两个网站都是 wordpress 网站,我也无法让插件以我需要的方式工作。

标签: .htaccess redirect mobile subdirectory


【解决方案1】:
RewriteRule ^(.*)$ /m/ [L,R=302]

替换字符串中缺少$1 反向引用。它应该是:

RewriteRule ^(.*)$ /m/$1 [L,R=302]

$1 反向引用包含RewriteRule 模式 中的捕获组(.*) 的内容(即URL 路径)。没有这个,它会将所有内容重定向到/m/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-13
    • 2014-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-21
    相关资源
    最近更新 更多