You must use parse_url to perform operations with the url. First analyze it, then make the changes you want using, for example, an explosion, then bring it back together.
$uri = "http://www.domain.com/something/interesting_part/?somevars&othervars"; $uri_parts = parse_url( $uri ); /* you should get: array(4) { ["scheme"]=> string(4) "http" ["host"]=> string(14) "www.domain.com" ["path"]=> string(28) "/something/interesting_part/" ["query"]=> string(18) "somevars&othervars" } */ ... // whatever regex or explode (regex seems to be a better idea now) // used on $uri_parts[ "path" ] ... $new_uri = $uri_parts[ "scheme" ] + $uri_parts[ "host" ] ... + $new_path ...
source share