Read current URL and change subdirectory?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Dror
Forum Newbie
Posts: 3
Joined: Sun Jun 21, 2009 3:50 pm

Read current URL and change subdirectory?

Post 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!
St1ckman
Forum Newbie
Posts: 7
Joined: Fri Jun 19, 2009 12:31 pm

Re: Read current URL and change subdirectory?

Post 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;
Dror
Forum Newbie
Posts: 3
Joined: Sun Jun 21, 2009 3:50 pm

Re: Read current URL and change subdirectory?

Post 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 :)
Dror
Forum Newbie
Posts: 3
Joined: Sun Jun 21, 2009 3:50 pm

Re: Read current URL and change subdirectory?

Post 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.
Post Reply