I have a PHP web application that uses a front controller pattern, with "pretty URLS", e.g.http://example.com/home/index/params
The app uses clean URI's - with Apache rewriting behind the scenes,e.g. the above request would be redirected internally to
http://example.com/index.php?url=home/index/params
However in this case the browser thinks that content is coming from a document called params
in a directory called index
and therefore any other requests, e.g. style.css
would be made to
`http://example.com/home/index/style.css`
Which of course is wrong, so I am using a variable in PHP to hardcode path requests
// constants.php
define('BASE_URL', 'http://localhost/03_my_website_projects/v4/public/')
// views.php
<link href="<?php echo BASE_URL; ?>styles.css" rel="stylesheet">
produces
<link href="http://localhost/03_my_website_projects/v4/public/styles.css" rel="stylesheet">
The problem is, when I try to visit the website on a mobile device over my local network, I cant view because all the links are hardcoded to an URL addresses that only work from the machine the project is being developed on, e.g. my mobile device would want to see a paths like
<link href="http://192.168.1.6/03_my_website_projects/v4/public/styles.css" rel="stylesheet">
How can I make URL's that are portable across machines and networks?
I know Laravel framework has got a really good solution for this (I wish I knew what it was) however this hacky solution is the only thing I could think of that would survive DHCP IP address reassignments.
// file: define_constants.php
$ip_address = shell_exec("ip addr show wlan0 | grep 'inet\s' | awk '{ print $2; }' | sed 's/\/.*$//'");
define('BASE_URL', 'http://' . $ip_address . '/03_my_website_projects/v4/public/');
now all links will use this new addressing, e.g.
<link href="http://192.168.1.6/03_my_website_projects/v4/public/styles.css" rel="stylesheet">