Ian O'Neill
Ian O'Neill

Reputation: 31

How can I redirect one url to another?

I have an 'api' so to speak where I am trying to proxy an api on another server for an App.

The target url is formed like: http://example.com/live/username/password/filename.mp4

how can I dynamically redirect to that?

here is the php I have so far which grabs the filename and user/password

<?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;
    }
?>

The app will read from the JSON provided and grab the 'filename' and then attempt to go to /live/ on the api host, but this will lead too http://example2.net/live/username/password/filename.mp4 which doesn't exist.

I thought about maybe something in htaccess?

Im trying to provide the App with the http://example.com/live/username/password/filename.mp4 not the http://example2.net/live/username/password/filename.mp4

(The example2.net is the $dns in the PHP)

Sorry I know this is a bad explanation - hopefully I've explained enough to be able to find an answer. (1st post)

edit: I have been reading about turing folders in links to querystring where I can point to this

<?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);
?>

I just need now to figure out how to use htaccess to turn the requested folder url into a query string from

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

Upvotes: 0

Views: 273

Answers (1)

Ian O&#39;Neill
Ian O&#39;Neill

Reputation: 31

I ended up using this which worked for .htaccess

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

Upvotes: 2

Related Questions