php - 如何将一个网址重定向到另一个网址?
问题描述
可以这么说,我有一个“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
解决方案
我最终使用了这个适用于 .htaccess
RewriteEngine on
RewriteRule ^live\/([^\/]+)\/([^\/]+)\/([^\/]+) /path/to/api/live.php?username=$1&password=$2&streamid=$3 [L]
推荐阅读
- python - 将闭合轮廓(点数组)转换为实体 3d 平面
- python - 如何在不影响python中的(引用)地址的情况下更改列表的内容?
- r - 如何使用单选按钮来改变 R Shiny 应用程序中的下拉菜单?
- sat - SAT 正常结膜形式
- firebase - 在另一个(onPressed)函数中使用来自 Firebase 数据的 ListView 的键
- python - 有没有办法在时间戳上加入两个具有偏移量的数据集,以便将 time_1 与 time_2 连接起来,其中 time_2 比 time_1 早 2 小时?
- javascript - 使用 match.params 时如何停止重新加载或重新渲染组件
- fortran - 我的代码给了我大约 10^-38 的随机数,但我没有使用任何随机函数
- regex - 添加用户代理过滤器后,codeigniter .htaccess 文件无法访问 css 和 js 文件,给出 404
- postgresql - 如何在 PostgreSQL 中使用相似的列连接两个表并将不匹配的字段保留为 NULL 或保持不变