riykrsh
riykrsh

Reputation: 77

Redirect a web page URL to another website

I have a simple PHP website. Let's call it youtubex.com. I want to redirect youtubex URLs (in the format shown on STEP2) to my website in the format shown on STEP3. Here, I am using YouTube, just for illustration.

STEP1: https://www.youtube.com/watch?v=lj62iuaKAhU
STEP2: https://www.youtubex.com/watch?v=lj62iuaKAhU 
STEP3: https://www.youtubex.com/#url=https://www.youtube.com/watch?v=lj62iuaKAhU 

STEP1 shows any desired URL. STEP2 shows the same URL from STEP1 with youtubex as domain. STEP3 shows the final required URL. I am trying to redirect STEP2 to STEP3.

I tried finding some solutions to this on the internet and SO, but, none help. Here is one.

Upvotes: 1

Views: 109

Answers (3)

Moussab Kbeisy
Moussab Kbeisy

Reputation: 606

using php str_replace and header :

$step2_url = "https://www.youtubex.com/watch?v=lj62iuaKAhU";
$part2_url = str_replace("youtubex","youtube",$step2_url);//the output is : https://www.youtube.com/watch?v=lj62iuaKAhU
$step3_url = "https://www.youtubex.com/#url=".$part2_url; //the output is : https://www.youtubex.com/#url=https://www.youtube.com/watch?v=lj62iuaKAhU

now you have the final url , simply redirect

header($step3_url);

Upvotes: 2

OMi Shah
OMi Shah

Reputation: 6186

This should do the work:

RedirectMatch 301 /watch$ https://www.youtubex.com/#url=https://www.youtube.com/watch

Upvotes: 3

Pooya Estakhri
Pooya Estakhri

Reputation: 1289

An inefficient but full php solution can be using the location header in php :

    $vid = $_GET['v']
    if($vid){  header("location:https://www.youtubex.com/#url=https://www.youtube.com/watch?v=$vid");
    }

by the way you can't get the text after the hash mark in php because it is not sent to the server.

javascript can do it in a more neat way without the second reload by checking window.location.href to see if the hash does not exist already and then get the v parameter in url then change url without refreshing the page by using window.history.pushState({"html":response.html,"pageTitle":response.pageTitle},"", urlPath);

Upvotes: 2

Related Questions