s.poole
s.poole

Reputation: 131

How do I get absolute URL's to work locally?

Everything works how I want on my live website, but because I'm using absolute URL's in my header.php and footer.php they don't work when I'm working locally (XAMPP) - I can't figure out how to get it working on both.

Header.php

<div id="top"><center><img src="http://cosworth-europe.com/images/header.png" style="max-width:100%;"></center></div>
<header id="header" class="site-header" role="banner">
<div id="header-inner" class="container sixteen columns over">
<hgroup class="one-third column alpha">

</hgroup>
    <nav id="main-nav" class="two thirds column omega">
        <ul>
            <li>
                <a href="http://www.cosworth-europe.co.uk/index.php">Home</a>
            </li>
            <li>
                <a href="http://www.cosworth-europe.co.uk/about-us.php">About Us</a>
            </li>
            <li>
                <a href="http://www.cosworth-europe.co.uk/news.php">News</a>
            </li>
            <li>
                <a href="http://www.cosworth-europe.co.uk/dealers.php">Dealers</a>
            </li>
            <li>
                <a href="http://www.cosworth-europe.co.uk/products.php">Products</a>
            </li>
            <li>
                <a href="http://www.cosworth-europe.co.uk/shop">Buy Online</a>
            </li>
            <li>
                <a href="http://www.cosworth-europe.co.uk/contactus.php">Contact</a>
            </li>
        </ul>
    </nav>

</div>
</header>

Code to grab header.php

<?php include($_SERVER['DOCUMENT_ROOT']."/header.php"); ?>

FTP Folder Structure **FTP Folder Structure**

Upvotes: 0

Views: 404

Answers (5)

Nelson Owalo
Nelson Owalo

Reputation: 2414

You should use relative urls, its recomended for portability.

</hgroup>
    <nav id="main-nav" class="two thirds column omega">
        <ul>
            <li>
                <a href="/index.php">Home</a>
            </li>
            <li>
                <a href="/about-us.php">About Us</a>
            </li>
            <li>
                <a href="/news.php">News</a>
            </li>
            <li>
                <a href="/dealers.php">Dealers</a>
            </li>
            <li>
                <a href="/products.php">Products</a>
            </li>
            <li>
                <a href="/shop">Buy Online</a>
            </li>
            <li>
                <a href="/contactus.php">Contact</a>
            </li>
        </ul>
    </nav>

</div>
</header>

Upvotes: 1

Thamilhan
Thamilhan

Reputation: 13313

If you want to make it work without editing your source file:

Actually, you shouldn't have done with absolute URL's, but still you can make it to work locally, you need small changes in hosts file. I don't know which OS you are currently in, so you can edit hosts like this:

Ubuntu:

In your terminal, type

sudo gedit /etc/hosts

You will be required to enter your password. In the file opened add this line to the end:

127.0.0.1   www.cosworth-europe.co.uk

Windows:

c:\windows\system32\drivers\etc\hosts

Open this in notepad with administrative privilege and add the above lines. Now local works in the similar way to your live website.

[But make sure to remove these lines, if you want to access live website (www.cosworth-europe.co.uk)]

See this for pictorial representation. In this method you need not change your files for now. But it is strongly recommended to use relative URLs for excellent portability!

Upvotes: 0

swidmann
swidmann

Reputation: 2792

You should not use absolute URLs, if you sometimes have to change something you will have a big mess with it.

You should use either a function or relative URLs.

Function example:

function getURL() {
    //get THIS ($develop) value from a config file
    $develop = true;

    return ( $develop ) ? "localhost" : "http://".$_SERVER["HTTP_HOST"];
}

i.e.

<a href="<?php echo getURL(); ?>/index.php">Home</a>

QUICKHELP: Just edit your hosts file like this:

127.0.0.1   www.cosworth-europe.co.uk

Relative URLs (relative from doc root in this example)

<ul>
    <li>
        <a href="/index.php">Home</a>
    </li>
    <li>
        <a href="/about-us.php">About Us</a>
    </li>
    <li>
        <a href="/news.php">News</a>
    </li>
</ul>

Upvotes: 1

Sougata Bose
Sougata Bose

Reputation: 31749

You can do two things.

First -

<a href="/index.php">Home</a> // The relative urls

Second -

Define a variable with the host name -

$host = ($_SERVER['HTTP_HOST'] === 'www.cosworth-europe.co.uk') ? 'http://www.cosworth-europe.co.uk' : 'http://localhost';

And

<a href="<?php echo $host;?>/index.php">Home</a>

Upvotes: 1

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167212

Always use relative URLs, at least to the domain.

<link rel="stylesheet" href="/css/style.css" />

If you really wanna use absolute URLs, set a variable as $homeroot and use it this way:

<link rel="stylesheet" href="<?php echo $homeroot; ?>/css/style.css" />

And you can declare it this way:

$homeroot = "http://my.example.com";

If you still want that to be more better, you can use this way:

$homeroot = $_SERVER["HTTP_HOST"];

Upvotes: 1

Related Questions