Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set base URL for all pages of my website?

Tags:

url

php

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?

like image 421
user2183116 Avatar asked Mar 18 '13 16:03

user2183116


People also ask

How do you define base URL?

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.

What is the base of all websites?

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.

How do I find my base URL?

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.


2 Answers

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.

like image 58
castis Avatar answered Oct 24 '22 10:10

castis


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>
like image 35
Martin Bean Avatar answered Oct 24 '22 11:10

Martin Bean