0

I searched "the whole" stackoverflow but didn't find a decent answer that works for me. I need to change the host of a url in php.

This url: http://example123.com/query?t=de&p=9372&pl=bb02799a&cat=&sz=400x320&scdid=e7311763324c781cff2d3bc55b2d83327aba111f2db79d0682860162c8a13c24&rnd=29137126

To This: http://example456.com/test?t=de&p=9372&pl=bb02799a&cat=&sz=400x320&scdid=e7311763324c781cff2d3bc55b2d83327aba111f2db79d0682860162c8a13c24&rnd=29137126

I only need to change the domain and the path or file, so far I've got this:

$originalurl = http://example123.com/query?t=de&p=9372&pl=bb02799a&cat=&sz=400....
$parts = parse_url($originalurl);
$parts['host'] = $_SERVER['HTTP_HOST'];
$parts['path'] = '/test';
$modifiedurl = http_build_query($parts);
print_r(urldecode($modifiedurl));

but it echos

scheme=http&host=localhost&path=/test&query=t=de&p=9372&pl=bb02799a&cat=&sz=400...

Please I don't want to use some strpos or something like that as I need it to be variable. Thanks ;)

1
  • because http_build_query() builds a query (funnily enough), and not a URL Commented Nov 13, 2015 at 9:19

4 Answers 4

1
$url = 'http://example123.com/query?t=de&p=9372&pl=bb02799a&cat=&sz=400x320&scdid=e7311763324c781cff2d3bc55b2d83327aba111f2db79d0682860162c8a13c24&rnd=29137126';
$query = parse_url($url)['query'];

$newUrl = 'http://www.younewdomain.com/path?' . $query;
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you that's almost exactly waht I want but this gets me the url right but the query gets a new line how can I fix this Thank you...
1

You'll have to do some concatenating manually. This works:

$originalurl = "http://example123.com/query?t=de&p=9372";
$parts = parse_url($originalurl);
$new_path = '/test';
$modifiedurl = $parts['scheme'] . "://" . $_SERVER['HTTP_HOST'] . $new_path . (isset($parts['query']) ? "?".$parts['query']:"");
print_r($modifiedurl);

Comments

1

Came up with a different approach:

$url = "http://example123.com/query?t=de&p=9372&pl=bb02799a&cat=&sz=400x320&scdid=e7311763324c781cff2d3bc55b2d83327aba111f2db79d0682860162c8a13c24&rnd=29137126";
$new_host = "http://newhost.com/blab";

//explode at ? so you get the query
$split = explode("?",$url,2);

//build new url
$new_url = $new_host."?".$split[1];

//finish
echo $new_url;

Comments

0

The reverse function of parse_url() should be http_build_url(), have you tried with it?

4 Comments

http_build_url() is not a standard function of PHP, its in the PECL extension
http_build_url may be the exact thing he want's but it's not always available => (PECL pecl_http >= 0.21.0)
it's available here though, github.com/jakeasmith/http_build_url, easy to get through composer
easy enough, but a little much for one function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.