Skip to content Skip to sidebar Skip to footer

Relative URL Issue

I will have multiple folders/modules to access common files. But accessing them seems to be big deal for me! I did gone through this link to understand the relative positioning an

Solution 1:

./css/style.css means from current directory and would achieve the same result as css/style.css. The easiest answer is to determine what the base path of your application is and use that. For instance, if your application is running as http://myapp.com, then you could set all your front-end paths to /css/style.css. If your app runs in a subdirectory, such as http://example.com/myapp, then your paths would be /myapp/css/style.css.

This does not apply the same on the PHP side. For them, you should really use document-relative paths. Having a PHP file that you include in multiple places in your app, the contents of which having something like include('../myDoc.php');, can lead to complications as the path isn't based on the included document's path, but rather the including. So using document-relative paths, you get around this include(__DIR__ . '/../myDoc.php');. Just something to consider if your app grows.


Solution 2:

Your PHP-includes seem to be correct. But in your HTML you need to change the linking to the CSS and JS Files (maybe even to your images).

You could use absolute paths:

<link rel="stylesheet" href="/css/style.css">
<script src="/js/script.js"></script>

the leading dot makes your paths relative to the HTML-Document, so if they are linked from a document in a subfolder, they point to a wrong location.


Solution 3:

Including files with

<?php
    include("page1.php")
?>

put the code (or content) from page1 into the caller page.

So you may have to detect from where your pages are called, or try absolute links (beginning by /)

I hope I answer you question correctly.


Post a Comment for "Relative URL Issue"