Slarti Posted September 12, 2007 Share Posted September 12, 2007 Hi I'm very new to php and have a problem I hope you can help with. At the moment I have a navigation menu inserted in to each page using the include command with the full URL hard coded in [http://www.example.com/some_page.php] I now have two domains [example.com + example.net] visitors using the .net end up on .com after using the nav menu as this is hard coded. I would like the visitor to stay on the .net or .com tld I thought I could use <?php $hostname = $_SERVER['SERVER_NAME']; ?> //or $hostname = $_SERVER['HTTP_HOST']; <a href="<?php echo $hostname; ?>/some_page.php">link to some page</a> This I thought would give http://example.com/some_page.php or http://example.net/some_page.php depending which tld was used. But I get http://www.example.com/www.example.com/some_page.php Where am I going wrong?? Thanx for any help. Quote Link to comment https://forums.phpfreaks.com/topic/69108-solved-possible-simple-question-but/ Share on other sites More sharing options...
DJTim666 Posted September 13, 2007 Share Posted September 13, 2007 All you have to do it use the filename. The persons browser should know what URL to go to depending on what is in the users address bar. So code like this. <?php echo "<a href='somefile.php'>somelink</a>"; ?> This will go to either domain depending on the URL in the users address bar. So if the user is at .com it will go to "example.com/somefile.php". If the user is at .net, it will go to "example.net/somefile.php". Hope that helps. -- DJ Quote Link to comment https://forums.phpfreaks.com/topic/69108-solved-possible-simple-question-but/#findComment-347417 Share on other sites More sharing options...
Psycho Posted September 13, 2007 Share Posted September 13, 2007 I agree with Tim, but to answer your question directly, the problem is that you are not prefacing the URL with "http://" so it is treated as a relative link and adding the root domin. This is what you should have used: <a href="http://<?php echo $hostname; ?>/some_page.php">Test 2</a> Quote Link to comment https://forums.phpfreaks.com/topic/69108-solved-possible-simple-question-but/#findComment-347420 Share on other sites More sharing options...
Slarti Posted September 13, 2007 Author Share Posted September 13, 2007 I agree with Tim, but to answer your question directly, the problem is that you are not prefacing the URL with "http://" so it is treated as a relative link and adding the root domin. This is what you should have used: <a href="http://<?php echo $hostname; ?>/some_page.php">Test 2</a> Thank you mjdamato this is exactly what I needed, I should have seen the problem as echoing $hostname; was giving the domain minus the http://. <?php echo "<a href='somefile.php'>somelink</a>"; ?> This will go to either domain depending on the URL in the users address bar. So if the user is at .com it will go to "example.com/somefile.php". If the user is at .net, it will go to "example.net/somefile.php". This does not work because the site is split into a number of directory's so the relative links to other files are different from each directory Quote Link to comment https://forums.phpfreaks.com/topic/69108-solved-possible-simple-question-but/#findComment-347628 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.