htaccess重定向取决于操作系统

时间:2020-05-27 21:28:12

标签: php wordpress apache .htaccess server

1-对于这种情况,我需要.htaccess代码。如果用户单击: website1。下载 要么 website2。下载 它将根据用户的操作系统进行重定向,例如:

2-我的主机中有多个网站,我需要为主机中的所有网站创建一个.htaccess文件,在哪里可以创建.htaccess文件?

这是htaccess代码,它不起作用:

<If "req('Host') = 'website1.download' && = 'website2.download'">
    # turn on rewrite engine
    RewriteEngine on

    # only detect smart phone devices if we are not on mobile site
    # to prevent redirect looping
    RewriteCond %{HTTP_HOST} !^https://google.com/$

    # Android
    RewriteCond %{HTTP_USER_AGENT} "android" [NC]
    # redirect to google play
    RewriteRule .? https://play.google.com/store%{REQUEST_URI}  [L,R=302]

    # iOS
    RewriteCond %{HTTP_USER_AGENT} "android" [NC]
    # redirect to app store
    RewriteRule .? https://www.apple.com/ios/app-store/%{REQUEST_URI}  [L,R=302]
</If>

1 个答案:

答案 0 :(得分:0)

好的,我要使用example.comexample.net,因为这就是它们的用途。另外,我假设这些域指向您的服务器,并且它们也都已绑定到该服务器。最后,我假设您已经具有用于设备检测的模式?您的问题并不十分清楚。 (如果您不这样做,则可能仅以Googlethis thread为基础。)

您可以堆叠RewriteCond并与OR一起加入

RewriteCond %{HTTP_HOST} example.com [NC,OR]
RewriteCond %{HTTP_HOST} example.net [NC]
RewriteCond %{HTTP_USER_AGENT} "android" [NC]
RewriteRule .? https://play.google.com/store%{REQUEST_URI}  [L,R=302]

从技术上讲,这也是一个正则表达式,因此应特别注意特殊字符,使其更加具体,添加子域支持,并将所有内容合并为一个条件。

RewriteCond %{HTTP_HOST} ^(www\.)?example\.(com|net)$ [NC]
RewriteCond %{HTTP_USER_AGENT} "android" [NC]
RewriteRule .? https://play.google.com/store%{REQUEST_URI}  [L,R=302]

您也应该注意HTTP_HOST vs SERVER_NAME

所有这些,我想不出这样做的充分理由。从SEO角度来看,您将要隐藏3个重定向中的2个,因为蜘蛛会将它们找到的信息存储在给定的URL中,如果改变,则以最近的更改为准。从用户的角度出发,大多数人都受过培训,可以单击商店中的徽章,我不会介绍一些新奇的东西。如果您想跟踪点击,我会使用JS。

相关问题