首页 > 解决方案 > 如何有条件地重定向到 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?

标签: phpapachemod-rewrite

解决方案


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

推荐阅读