Reputation: 35
I am creating a website that has to be edited on the local server and then uploaded later on the remote server. I'm using absolute path on the local server but then when I upload it to the remote server, links don't work anymore. It seems that I can only use either relative or absolute, not both.
I want the script to work on both local as well as remote server. Local is obviously for editing (sometimes I used to have slow internet connection so I have to use localhost). And Remote for production purposes and client preview.
Below is the function I created:
PHP SCRIPT:
define("LOCALHOST", "/adamsProject");
define("REMOTE_SERVER", "http://adamsproject.ph");
function host(){
if(!LOCALHOST){
echo REMOTE_SERVER;
}else{
echo LOCALHOST;
}
}
EXECUTION ON HTML:
<ul>
<a href="index.php"><img src=" <?php echo host(); ?> /images/logoMain.png" id="logoMain" > </a>
<li><a href=" <?php echo host(); ?> /index.php"> HOME </a></li><li>
<a href="#" id="servicesWide"> SERVICES <i class="fa fa-chevron-down"></i></a></li><li>
<a href=" <?php echo host(); ?> /underConstruction.php"> ABOUT US </a></li><li>
<a href=" <?php echo host(); ?> /pages/contactUs.php"> CONTACT US </a></li><li>
<a href="#" id="folioWide"> PORTFOLIO <i class="fa fa-chevron-down"></i> </a></li>
</ul>
Upvotes: 0
Views: 1679
Reputation: 303
Like DeDee said, You probably want to remove the echos in the link and also the spaces right after the 'host(); ?>' See the code below. Also, no need for the PHP function; you can use "$_SERVER['SERVER_NAME']" that grabs the current web server name. I hope this helps.
if(!$_SERVER['SERVER_NAME']='localhost'){
$domain = '/adamsProject';
} else {
$domain = 'http://www.adamsproject.ph';
}
?>
<ul>
<a href="<?php echo $domain;?>/index.php"><img src="<?php echo $domain;?>/images/logoMain.png" id="logoMain" > </a>
<li><a href="<?php echo $domain;?>/index.php"> HOME </a></li><li>
<a href="#" id="servicesWide"> SERVICES <i class="fa fa-chevron-down"></i></a></li><li>
<a href="<?php echo $domain;?>/underConstruction.php"> ABOUT US </a></li><li>
<a href="<?php echo $domain;?>/pages/contactUs.php"> CONTACT US </a></li><li>
<a href="#" id="folioWide"> PORTFOLIO <i class="fa fa-chevron-down"></i></a></li>
</ul>
EDIT Per your comments, if you have a sub directory off of you 'localhost' then you may need a if statement to add your 'adamsProject' directory to your domain when you are on local host. Try that. Let me know.
EDIT2
Replaced remote server domain with your hard coded domain. Also, added $domain
to your index.php on home img link and HOME nav link.
Upvotes: 1
Reputation: 53
Try using $ _ SERVER [ ' SERVER_NAME ' ] instead of your function. It will work both for local server and remote one. Your code should look like this.
<ul>
<a href="index.php"><img src="<?php echo 'http://' . $_SERVER['SERVER_NAME'];?>/images/logoMain.png" id="logoMain" > </a>
<li><a href="<?php echo 'http://' . $_SERVER['SERVER_NAME'];?>/index.php"> HOME </a></li><li>
<a href="#" id="servicesWide"> SERVICES <i class="fa fa-chevron-down"></i></a></li><li>
<a href="<?php echo 'http://' . $_SERVER['SERVER_NAME'];?>/underConstruction.php"> ABOUT US </a></li><li>
<a href="<?php echo 'http://' . $_SERVER['SERVER_NAME'];?>/pages/contactUs.php"> CONTACT US </a></li><li>
<a href="#" id="folioWide"> PORTFOLIO <i class="fa fa-chevron-down"></i> </a></li></ul>
It will return http://localhost/something
or http://yourwebsite.pl/something
You have to add http://
before using <?php echo $_SERVER['SERVER_NAME'];?>
. If you won't it will treat is as relative path anyway and will double your server name. In this case it's treated as absolute paths. Here is an example to understand it easier:
Relative path:
<a href="index.php"><img src="/images/logoMain.png" id="logoMain" > </a>
Still relative path(server name doubled)
<a href="<?php echo $_SERVER['SERVER_NAME'];?>/index.php"> HOME </a>
Absolute path (correct one):
<a href="<?php echo 'http://' . $_SERVER['SERVER_NAME'];?>/underConstruction.php"> ABOUT US </a>
*Remember that <?php echo $_SERVER['SERVER_NAME'];?>
return server name without /
in the end.
Upvotes: 1
Reputation: 303
Alternatively just use relative URLs... so,
<a href="/underConstruction.php"> ABOUT US </a>
Will work the same on both remote and local. Rendering the page produces:
http://localhost/underConstruction.php
for me on local.
Upvotes: 1