php - 如何有条件地重定向到 Apache 中的自定义方案 URL?
问题描述
我有一个这样配置的 Apache:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/launchApp$
RewriteCond %{HTTP_USER_AGENT} Android
# Here I want to redirect to an URL with a custom scheme
RewriteRule
不适用于自定义方案(在我的情况下mobile://...
)
PHP 模块已加载到我的 Apache 中,然后我想到了一个解决方案,例如重定向到提供以下文件的本地 URL:
<?php
header("Location: mobile://...");
?>
但是我不喜欢通过将 HTTP 请求转发到本地请求来实现这一点的想法,这会导致我的其余配置出现问题。
如何有条件地重定向到 Apache 中的自定义方案 URL?
解决方案
通过直接引用文件系统路径,我找到了一个令人满意的解决方案,它使用 shell 脚本并且不允许代理请求:
我已经创建了文件/var/www/cgi-bin/redirectToMobile.sh
:
#!/bin/sh -f
echo "Status: 302 Found"
echo "Location: mobile://..."
echo ""
然后是以下 Apache conf:
RewriteCond %{REQUEST_URI} ^/launchApp$
RewriteCond %{HTTP_USER_AGENT} Android
RewriteRule ^.*$ /var/www/cgi-bin/redirectToMobile.sh [L]
ScriptAlias /cgi-bin/ /var/www/cgi-bin/
<Directory "/var/www/cgi-bin">
AddHandler cgi-script .sh
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
推荐阅读
- javascript - 从目录导入所有 json 文件不起作用
- android - 返回绑定数据或回收器视图问题
- swift - 如何将选择器绑定到 NSObject 中的方法
- java - 如何使用实现父接口的类中的构造函数初始化接口类型的对象?
- git - 如何在本地删除 git 文件而不从远程存储库中删除它们?
- r - 匹配单词字符串并返回不匹配的单词
- spotfire - Spotfire 自定义表达式从列中添加值
- azure - 如何在 Microsoft Azure Vault 中存储密钥库?
- android - 如何使用天气 API 和 JSON 数据来确定当前时间是白天还是晚上(android)
- docker - 带有 JHipster 和 Docker 注册表的 Gitlab CI/CD