简体   繁体   中英

How to add to php get home url to make links to new pages?

I'm currently working on a website that's hosted on our development server, I want to make some links that won't break when I move the site onto the new domain, so I plan to use to first get the root url, but how can I use that to make a link to the page at www.domain.com/blog?

Thanks a lot, hope that makes sense, couldn't really find a better way to word it!

Thanks, Ethan

You can either use relative paths or $_SERVER['HTTP_HOST'];
For example :

$baseUrl = "http://" . $_SERVER['HTTP_HOST'];
$myLink = $baseUrl."/mypage";

Define in a common script like db.php / config.php

$application = 'local';
//$application = 'web';

if($application == 'local'){
    define("SITEROOT","localhost/domain.com/");
    $websitelink = "http://".constant("SITEROOT");
}
if($application == 'web'){
    define("SITEROOT","www.domain.com/");
    $websitelink = "http://".constant("SITEROOT");
}

Now in your menubar or where you want to add link to any page..eg for blog..

<a href="<?php echo $websitelink;?>blog">Go To Blog</a>
<a href="<?php echo $websitelink;?>forum">Go To Forum</a>
<a href="<?php echo $websitelink;?>aboutus.php">About Us</a>

When you go online , simply disable $application = 'local' and comment out $application='web' like below

//$application = 'local';
$application = 'web';

With this approach, you can define many parameters and can use them anywhere in website....Just changing $application does the trick...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM