Search code examples
phpurllocalhosthrefproduction-environment

Safe way to write URLs that transfer across environments


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?


Solution

  • 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>.