1
votes

I am currently developing some software on my local machine. For the sake of this question, lets call the software StackOverflow. I have the below pages

C:\sites\StackOverflow\index.php
C:\sites\StackOverflow\content\page1.php
C:\sites\StackOverflow\content\page2.php
C:\sites\StackOverflow\content\page3.php

In my current environment going to http://localhost/StackOverflow/index.php works successfully. And the URL's I've written for the content pages would look similar to http://localhost/StackOverflow/content/page1.php.

When I deploy this to production however the URL will no longer be local host but rather an IP or a hostname that has yet to be decided. How should I be writing my URLs so that I have to do as little work when I deploy as possible?

6

6 Answers

2
votes

Start them relative to the root: /.

For example:

/StackOverflow/index.php
/StackOverflow/content/page1.php
/StackOverflow/content/page2.php
/StackOverflow/content/page3.php

They will automatically be prefixed with the current domain or whatever you set as the <base>.

1
votes

you don't need an absolute http path, it's actually potentially problematic if you use HTTPS as either one of those links still pointing to HTTP will potentially cause browsers to show a "unsafe content" error

// This allows you to deploy to different enviroments where some might
// have the page _not_ in the site root, without having to edit
// 1000s of lines of code.
$config['baseurl'] = '/StackOverflow';

// then when you echo an url you can just
echo $config['baseurl'] . '/content/page1.php';
1
votes

I usually set up a $baseurl and a $basepath variable in my app's config file pointing to my app's root directory and in my front-end i just prefix my urls with these variables.

This way you can transfer your site from one server to an other easily and it also works with apps in subdirectories

1
votes

I always assign a couple of constants in a config file and base all of my paths off of that.

 if ($_SERVER["APP_ENVIRONMENT"] == "development"){
   define("BASE_PATH", "C:/sites/StackOverflow/development/");
   define("BASE_URL", "/development/");
 } else {
   define ("BASE_PATH", "/home/www.mysite.com/htdocs/");
   define ("BASE_URL", "/");
 }

Then when doing url I construct the path like

 $url = BASE_URL."path/to/whatever.jpg";

And for file references

 include (BASE_PATH."libraries/mylib.php");

You can set the $_SERVER["APP_ENVIRONMENT"] in the htaccess on a per-environment basis using PutEnv

SetEnv APP_ENVIRONMENT development
1
votes

Use always relative URLs, to be able to transfer the site not only between servers, but also between directories in the same server.

Relative usually starts from you current file location. Usually I have my index.php including the other files. So, all it's relative to the root location:

./ means same path
./test/ means one level up
../ means 1 level-below
../../ means 2 levels-below etc

See

http://www.webdevelopersnotes.com/design/relative_and_absolute_urls.php3

1
votes

If you want it dynamically you can do it like this:

$pathInfo = pathinfo($_SERVER['SCRIPT_NAME']);
$baseUrl = rtrim($pathInfo['dirname'], "\\/");

I save this baseUrl in my request object and use it in my view scripts. This is up to you..

Use it like this:

<a href="<?php echo $baseUrl; ?>/content/page1.php">Test</a>

Sometimes you'll need the absolute url if you send an email for example and need to link back to the site:

$rootUrl = 'http';
if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') {
    $rootUrl .= 's';
}
$rootUrl .= '://'.$_SERVER['SERVER_NAME'];
if ($_SERVER['SERVER_PORT'] != '80') {
    $rootUrl .= ':'.$_SERVER['SERVER_PORT'];
}
$rootUrl .= $baseUrl;

Use $rootUrl in the same way like $baseUrl.