How do I set a base URL for my website and get it to include in every page?
Is there a way for me to easily change a variable to be the base url for the website, such as <?php $baseurl = "http://www.website.com/website/"; ?>
, and include this on every page so that all CSS, JavaScript, images and PHP includes follow this $baseurl
?
The URL found in the address bar of the front page of a website is its base URL. In other words, the common prefix found while navigating inside a given website is known as the base URL. One can select a base URL from the list of those available with help of the URL general properties page.
On the web the base unit for every website or document which contains information is web page. Explanation: A Web page, often known as a page, is a report that is displayed in Internet browsers and is usually written in HTML. A URL address can be entered into the browser's location bar to access a site page.
To find the base URL of your website, go to the site's front page. What you see in the address bar on your site's front page is the base URL of your website.
You may want to take a look at the html base tag.
Inside the <head>
section of your html, put
<base href="http://www.website.com/website/">
On top of that, you may want to have a base.php
with default directories and whatnot that you include into your project.
You can’t make both PHP and client-side assets use the same base URL, unless you use PHP to echo
a base URL variable or constant to the page.
The usual approach is to have a bootstrap file that you include on every page, and define your base URL and other site-wide variables in there.
bootstrap.php:
<?php
define('BASE_URL', 'http://example.com');
index.php:
<?php
include('bootstrap.php');
?>
<!DOCTYPE html>
<html>
<head>
<!-- // -->
<link rel="stylesheet" href="<?php echo BASE_URL; ?>/css/styles.css" />
</head>
<body>
<!-- // -->
</body>
</html>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With