I've got a bit of a dilemma, and it's been bothering me for quite some time. I have a local testing server that's set up like so: 127.0.0.1/
My website in offline mode looks like this:
127.0.0.1/websitename/index.php
My live version of the website looks like this:
websitename.com/index.php
I've got the base script for almost all of the links, except for the including header and footer files.
The links in the footer and header files work except for the home page (on the root of the website).
Could anyone redirect me to a proper method of doing multi sub directory base URL linking for both offline and online?
I’ve tinkered around with most of the $_SERVER[]
tags and attributes, as well as parse_url()
.
Don’t tinker with them. There’s no clean/automated way to do what you are doing. Just set a base path manually in a config file & don’t worry about it—relative paths—ever again. And if you need to set a base URL, the process is similar.
So as far as a file base path goes, you should explicitly set a
$BASE_PATH
like this:If you don’t know what your file system base path is, just place this line of code in your PHP code; like
index.php
:Then load that page. Somewhere near the top will be this text:
Then with that set you can change your code to be something like this:
And then set your
include_once
like this:Some might say you should use
$_SERVER['DOCUMENT_ROOT']
or evendirname(__FILE__)
directly with the implication being that you can simplify code portability that way. But the way file paths are set for installs can vary so it just never works well & the chances of you getting snagged on an odd server quirk is high.It’s always best to just to manually set a
$BASE_PATH
in a config file when you move code than deal with the headaches caused by PHP constants like$_SERVER
not being consistent between installs, setups & configurations.And as far as a base URL goes, just follow the same thinking with this being on your local development setup:
And this being on your production server:
So with that
$BASE_URL
set then you can just do this:Now just prepend any path you might need requested via a URL with
$BASE_URL
& you should be good to go.I suggest you move to a development environment which more closely reflects the live system. For this, you can run a WAMP server and configure it to serve your web site as a domain like
mysite.local
and then you simply edit your hosts file so thatmysite.local
resolves to your127.0.0.1
. Then you just type mysite.local into your browser, it resolves to your local PC, and make sure apache is configured for virtual hosts and listening on port80
.Your hosts is a local DNS lookup file found in
windows\system32\drivers\etc
. You may need to open it in Notepad which is run as administrator in order to be able to edit it.There's an easy and cool thing you can do in local server called: Virtual host, which allows you to create subdomain to access your local website without entering subdirectories in the url.
Example:
You can do something like the following to access your files:
mysite.localhost/
which is exactly the same aslocalhost/mysite/index.php
In that way, you don't have to worry about subdirectories when moving you website to the online server.
Links for virtual host:
WAMP
XAMPP