If the domain is: site.com then you can write it like this:

<script src="http://site.com/asset/style.css"></script> 

BUT if the user has thrown the archive of the site into a subfolder, then this option is no longer suitable.

You can register as:

 $url = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; 

There will be such a link:

 <script src="http://site.com/подпапка/asset/style.css"></script> 

But if the user goes into another folder, then this option is not suitable either, for example: http://site.com/subfolder/admin/ styles will be:

 <script src="http://site.com/подпапка/admin/asset/style.css"></script> 

it should be like this:

 <script src="http://site.com/подпапка/asset/style.css"></script> 

How to do it correctly, what would the links work in folders and subfolders, etc? For example, in the wordpress engine everything works as it should. I did not find the code I found as they did, I want to do the same for myself.

  • On the page you specify the base tag, and all styles are loaded along a relative path - BOPOH
  • Relative links do not roll? Pag of the second variant substitute only the main domain and it is not important that the user is in the folder or subfolder - jackrv

2 answers 2

Set a variable in the config of your engine or set the constant equal to url to the site folder, for example:

 define('BASE_URL', 'http://site.com/подпапка/'); 

And where you need absolute links to files, use it, for example:

 <script src="<?php echo BASE_URL; ?>asset/style.css"></script> 

    And why do you declare links to styles via script tags? After all, you can simply <link href="/asset/style.css" rel="stylesheet"> <head> pages write <link href="/asset/style.css" rel="stylesheet"> and they will be loaded regardless of the paths. But it is provided that asset is in the root