Page 1 of 1

Read current URL and change subdirectory?

Posted: Sun Jun 21, 2009 4:03 pm
by Dror
Hi Guys!

I'm still a very fresh newbie on PHP and my problem seems really simple but I can't figure it out. :banghead:

I have a website with the url http://www.mysite.com/a/index.html
Now I want to insert a link with PHP which changes the directory from 'a' to 'b', so the url of the link should look like http://www.mysite.com/b/index.html
The file name and the rest of the URL stay the same, it's really only the directory that's supposed to change.

Thanks in advance!

Re: Read current URL and change subdirectory?

Posted: Sun Jun 21, 2009 4:23 pm
by St1ckman
Something like this?

$linka = 'http://www.mysite.com/a/index.html';
$linkb = 'http://www.mysite.com/b/index.html';
if ($linka != $linkb){ $linka = $linkb; }
echo $linka;

Re: Read current URL and change subdirectory?

Posted: Sun Jun 21, 2009 5:33 pm
by Dror
Uhm... no.
It should get the current URL (I guess with the PHP Self comand) and replace the directory (a to b).
This way it would work for all file names.

Thanks though for any help :)

Re: Read current URL and change subdirectory?

Posted: Sun Jun 21, 2009 6:27 pm
by Dror
Okay I actually figured it out myself.

<?php
$myUrl = "http://".$SERVER_NAME.$REQUEST_URI;
$alternativeUrl = preg_replace("/en/","de",$myUrl);
echo "<a href= ".$alternativeUrl."> Change Language </a><br><br>";
?>

With "preg_replace("/en/","de",$myUrl);" you choose what part is to be replaced with what.