首页 > 解决方案 > 如何将一个网址重定向到另一个网址?

问题描述

可以这么说,我有一个“api”,我试图在另一台服务器上为一个应用代理一个 api。

目标网址的格式如下: http ://example.com/live/username/password/filename.mp4

我怎样才能动态重定向到那个?

这是我到目前为止的 php,它获取文件名和用户/密码

<?php
    if (isset($_GET['username']))
    {
        $username=$_GET['username'];
        $password=$_GET['password'];
    }
    else if (isset($_POST['username']))
    {
        $username=$_POST['username'];
        $password=$_POST['password'];
    }
    $action = isset($_GET['action']) ? $_GET['action'] : '';

    $db = new SQLite3('./.dns.db');
    $res = $db->query('SELECT * FROM dns');

    $arr = array();

    while ($row = $res->fetchArray(SQLITE3_ASSOC)) 
    {
        $arr[] = $row['url'];
        foreach ($arr as $value) 
        {
            $api_call = $value.'/player_api.php?username='.$username.'&password='.$password;
            $api = json_decode(file_get_contents($api_call), TRUE);
            $api2 = json_decode(json_encode($api["user_info"]) ,TRUE) ;
            $auth = $api2["auth"];
            if ($auth == 1) 
            {
                $dns = $value;
            } 
        }
    }

    if (isset($_GET['vod_id']) && $_GET['vod_id'] !== "") 
    {
        $vodid = $_GET["vod_id"];
        $vodinfo = file_get_contents($dns.'/player_api.php?username='.$username.'&password='.$password.'&action=get_vod_info&vod_id='.$vodid);
        echo $vodinfo;

    } 
    else if (isset($_GET['series_id']) && $_GET['series_id'] !== "") 
    {
        $seriesid = $_GET["series_id"];
        $seriesinfo = file_get_contents($dns.'/player_api.php?username='.$username.'&password='.$password.'&action=get_series_info&series_id='.$seriesid);
        echo $seriesinfo;
    } 
    else if ($action !== "") 
    {
        $get_actions = file_get_contents($dns.'/player_api.php?username='.$username.'&password='.$password.'&action='.$action);
        echo $get_actions;
    } 
    else 
    {
        $login = file_get_contents($dns.'/panel_api.php?username='.$username.'&password='.$password);
        echo $login;
    }
?>

该应用程序将从提供的 JSON 中读取并获取“文件名”,然后尝试转到 api 主机上的 /live/,但这也会导致http://example2.net/live/username/password/filename.mp4 这不存在。

我想到了htaccess中的一些东西?

我试图为应用程序提供http://example.com/live/username/password/filename.mp4 而不是 http://example2.net/live/username/password/filename.mp4

(example2.net 是 PHP 中的 $dns)

抱歉,我知道这是一个不好的解释——希望我已经解释得足够多,能够找到答案。(第 1 篇文章)

编辑:我一直在阅读关于查询字符串链接中的图灵文件夹,我可以在其中指向这个

<?php
    if (isset($_GET['username']))
    {
        $username=$_GET['username'];
        $password=$_GET['password'];
        $streamid=$_GET['streamid'];
    }
    else if (isset($_POST['username']))
    {
        $username=$_POST['username'];
        $password=$_POST['password'];
        $streamid=$_POST['streamid'];
    }
    header('Location: http://example.com/live/'.$username.'/'.$password.'/'.$streamid);
?>

我现在只需要弄清楚如何使用 htaccess 将请求的文件夹 url 转换为查询字符串

example2.net/live/username/password/file.mp4 到 example2.net/live.php?username=username&password=password&streamid=streamid.mp4

标签: php.htaccessproxyreverse-proxy

解决方案


我最终使用了这个适用于 .htaccess

RewriteEngine on
RewriteRule ^live\/([^\/]+)\/([^\/]+)\/([^\/]+) /path/to/api/live.php?username=$1&password=$2&streamid=$3 [L]

推荐阅读