使用 htaccess 将 URL 重定向到特定于应用程序的深层链接 [英] Use htaccess to redirect URL to app-specific deep-link

查看:35
本文介绍了使用 htaccess 将 URL 重定向到特定于应用程序的深层链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试执行以下操作:

用户访问带有查询参数的 URL:http://www.example.com/?invite=1234

然后我希望他们能够深入链接到他们 iOS 设备上的应用,所以他们去:app_name://1234

有关如何在我的 .htaccess 文件中完成此操作的任何建议?

我试过了,但没有用:

RewriteEngine On # 开启重写引擎RewriteRule ^invite/(.*)/$ app_name://$1 [NC,L]

如果 RewriteRule 不起作用,谁能给我发送一个 RewriteCond 或 JavaScript 的示例代码来实现我的需要?

解决方案

不确定这将如何与 iOS 设备配合使用,但无论如何...

<块引用>

RewriteRule ^invite/(.*)/$ app_name://$1 [NC,L]

这与给定的 URL 不匹配.这将匹配 example.com/invite/1234/ 形式的请求 URL.但是,您也在匹配任何内容 - 您的示例 URL 仅包含数字.

RewriteRule pattern 仅匹配 URL 路径,您需要使用 RewriteCond 指令来匹配查询字符串.因此,要匹配 example.com/?invite=1234(其 URL 路径为空),您需要执行以下操作:

RewriteCond %{QUERY_STRING} ^invite=([^&]+)重写规则 ^$ app_name://%1 [R,L]

%1 反向引用指向最后匹配的 CondPattern.

我也将 invite 参数值限制为至少 1 个字符 - 或者您真的希望允许空参数值通过?如果该值只能是数字,那么您应该将模式限制为只有数字.例如.^invite=(\d+).

我已经包含了 R 标志 - 因为这必须是一个外部重定向 - 如果它要起作用的话.

然而,除非 Apache 知道 app_name 协议,否则这可能根本不起作用.如果不是,则它只会被视为相对 URL 并导致格式错误的重定向.

I am trying to do the following:

User visits URL with query parameter: http://www.example.com/?invite=1234

I then want them to be deep linked into the app on their iOS device, so they go to: app_name://1234

Any suggestions on how to accomplish this in my .htaccess file?

I tried this but it doesn't work:

RewriteEngine On # Turn on the rewriting engine

RewriteRule ^invite/(.*)/$ app_name://$1 [NC,L]

If RewriteRule won't work, can anyone send me an example code for RewriteCond or JavaScript to achieve what I need?

解决方案

Not sure how this will work with the iOS device, but anyway...

RewriteRule ^invite/(.*)/$ app_name://$1 [NC,L]

This doesn't match the given URL. This would match a requested URL of the form example.com/invite/1234/. However, you are also matching anything - your example URL contains digits only.

The RewriteRule pattern matches against the URL-path only, you need to use a RewriteCond directive in order to match the query string. So, to match example.com/?invite=1234 (which has an empty URL-path), you would need to do something like the following instead:

RewriteCond %{QUERY_STRING} ^invite=([^&]+)
RewriteRule ^$ app_name://%1 [R,L]

The %1 backreference refers back to the last matched CondPattern.

I've also restricted the invite parameter value to at least 1 character - or do you really want to allow empty parameter values through? If the value can be only digits then you should limit the pattern to only digits. eg. ^invite=(\d+).

I've include the R flag - since this would have to be an external redirect - if it's going to work at all.

However, this may not work at all unless Apache is aware of the app_name protocol. If its not then it will simply be seen as a relative URL and result in a malformed redirect.

这篇关于使用 htaccess 将 URL 重定向到特定于应用程序的深层链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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