如何在Apache中有条件地重定向到自定义方案URL?

时间:2019-04-05 12:41:56

标签: php apache mod-rewrite

我有一个像这样配置的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?

1 个答案:

答案 0 :(得分:0)

通过直接引用文件系统路径,我找到了一个令人满意的解决方案,该解决方案使用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>