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

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

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Centralizing Base URLs for Static and Dynamic Assets

Setting a base URL that applies globally across every page of your website is a fundamental requirement for robust asset management. The goal is to ensure that all CSS, JavaScript, images, and PHP includes resolve correctly regardless of the subdirectory or query string used to access a specific resource. While the concept seems simple—defining a single variable like $baseurl in a root file—the implementation varies significantly depending on your stack and whether you are serving static files or rendering dynamic views.

In many legacy PHP applications, developers often initialize this value in index.php or a bootstrap file using a hardcoded string:

<?php $baseurl = "http://www.website.com/website/"; ?>

This approach works for simple scripts but fails to scale when the application is deployed behind a load balancer, runs on multiple servers, or utilizes a reverse proxy. Hardcoding an absolute path ties your application to a specific hostname and port, making it brittle during deployment or if you switch from HTTP to HTTPS. Instead of relying on environment variables that might be forgotten in configuration files, consider leveraging the server's ability to detect its own address. In PHP, functions like $_SERVER['HTTP_HOST'] combined with the script name can dynamically construct the base path without hardcoding it.

However, if your goal is to use this variable across all pages efficiently, you must determine where that variable lives in your architecture. If you are using a framework like Laravel, the concept of a "base URL" shifts slightly from a simple PHP variable to a configuration directive managed by the APP_URL environment variable or the config/app.php file. In this context, the base URL is not just a string; it dictates how asset helpers and routing functions behave.

For static sites or custom PHP builds where you need to inject this path into every page manually, placing the definition in a shared layout file or a specific include directive is essential. A common pattern involves defining the base in the root index file and then including it at the top of your layout templates:

// In your root layout or header include
$base = rtrim($_SERVER['REQUEST_URI'], '/');
// Ensure we don't double slash issues
if (empty($base)) {
    $base = '/';
}

This dynamic approach ensures that whether a user accesses /website/ or /website/about, the base URL reflects the actual path served by the server. When linking to assets, you can then use this variable consistently: <link rel="stylesheet" href="$base/css/style.css">.

One significant pitfall in this process is handling subdirectories. If your application lives inside a folder named website within the document root, your relative paths will break if you don't account for that prefix. Using $_SERVER['DOCUMENT_ROOT'] alongside $_SERVER['SCRIPT_FILENAME'] allows you to calculate the relative path to your public folder programmatically, rather than guessing the string literal.

When dealing with modern stacks like Laravel, relying on Blade directives such as {{ asset('css/style.css') }} automatically handles this logic. The framework appends the configured base URL to these assets without you needing to manually inject a PHP variable into every single tag. This abstraction is superior because it prevents duplication errors and ensures consistency even if your server configuration changes. If you are maintaining a legacy system where such helpers aren't available, encapsulating the logic in a utility class or a helper function called at the very start of the page lifecycle is a clean compromise.

Ultimately, the choice between hardcoding a string, using dynamic server variables, or leveraging framework abstractions depends on your deployment strategy. Dynamic detection offers the highest resilience against environment changes, while framework-native solutions provide the most maintainable codebase for larger applications. Avoid mixing static strings with dynamic paths in the same application, as this leads to inconsistent asset loading and broken layouts across different environments.

Our network