dtyqeoc70733 2015-08-11 17:01
浏览 96
已采纳

如何让绝对路径在本地和远程服务器上运行?

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>

  • 写回答

3条回答 默认 最新

  • dtnmnw3697 2015-08-11 17:32
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀