Jump to content

[SOLVED] Possible simple question but...


Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/69108-solved-possible-simple-question-but/
Share on other sites

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

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>

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.