0

Following on from a question I asked yesterday that I ended up finding a solution for, I've now run into another problem that I've been trying to solve with no luck.

I'm testing php files with XAMPP and in order to allow stylesheets in header.php to work everywhere, I used the following line in the header.php file.

<base href="http://localhost/folder-name-here/">

It works while I'm testing locally, however, I'm working on a site cleanup for someone else as practice (I'm a beginner) and I've already assumed that this won't work if the site is pushed to the server - the person I'm working with has also asked why I don't use something relative to the root.

I've seen various suggestions online, like using:

<base href="~/>

or

<base href="/">

but unfortunately, none of these are working.

Not sure of what else I could try at this point. Any ideas?

Sophie
  • 41
  • 5
  • As you have tagged `php` - remove `` and instead use a config file that defines a `URL` constant `define('URL', 'http://website.localhost.de');` that defines the path. Then do ` – Xatenev Feb 07 '19 at 12:01
  • what is not working? maybe the root is set wrong? check this out https://stackoverflow.com/questions/7229783/difference-between-serverdocument-root-and-serverhttp-host – mrQubeMaster Feb 07 '19 at 12:02
  • check what is your document root and where the file is located. if file is inside document root then / should work. if possible share the folder structure of your project and document root – Gyaneshwar Pardhi Feb 07 '19 at 12:02
  • @GyaneshwarPardhi My header.php and index.php are in the same main folder - the css is in /assets - I basically want my header.php to work regardless of whether I use it on index.php or something like /the-team/folder/about-us.php – Sophie Feb 07 '19 at 12:17
  • ok so if you are adding the header.php in index.php, path will be heaerd.php but if you are adding to about-us.php which is inside /the-team/folder/about-us.php then path will be different it should be ../header.php – Gyaneshwar Pardhi Feb 07 '19 at 12:23
  • @mrQubeMaster basically as mentioned in my previous comment, I'm happy with the result of my initial solution but wonder if there is a way to change the href so it works when it's uploaded to a server. The info in the link is going over my head a bit as I'm truly a beginner but I'll do some more reading and see if I can figure it out - thanks. – Sophie Feb 07 '19 at 12:23
  • @GyaneshwarPardhi yes I'm aware that it should change to ../header.php or ../../header.php - depending on where it is. However, unless I have a defined base href in my header.php for the linked CSS, the CSS breaks and doesn't work unless it's in the main/root folder. This is where the base tag helped me - so the css links would be the same, regardless of where I included header.php, I'm just trying to figure out if I can use a base href that will work whether I'm testing locally or pushing to the server. – Sophie Feb 07 '19 at 12:27

1 Answers1

0

Instead of the base tag i would use $_SERVER['HTTP_HOST'], this returns the base url from your site (localhost) or when it is online the website (example.com).
I picked the link from your previous post and modified it.

<link rel="stylesheet" type="text/css" href="<?php echo $_SERVER['HTTP_HOST']; ?>/assets/sf.css"/>

Ofcourse if this is not working as you like you could also create a php variable for it that you modify

<?php
$baseUrl = 'localhost/folder';
$baseUrl = $_SERVER['HTTP_HOST'].'folder';
?>

<link rel="stylesheet" type="text/css" href="<?php echo baseUrl; ?>/assets/sf.css"/>

this way it is easy to adjust all the links at once when you deploy to a webserver

edit:
for when discussion chat room closes this was the solution that kinda worked,

<link rel="stylesheet" type="text/css" href="//<?php echo $_SERVER['HTTP_HOST']; ?>/kinderwunschpraxis_site/assets/sf.css"/>
mrQubeMaster
  • 342
  • 2
  • 14
  • Thanks for the input! I've tried both suggestions and both broke the css :( – Sophie Feb 07 '19 at 13:19
  • au.. totally though that would work. how do you have the web server setup? how is the file structure? is it a 404 error you are getting or something else? – mrQubeMaster Feb 07 '19 at 13:25
  • from the localserver it's basically: localserver/kinderwunschpraxis-site/ index.php header.php footer.php sidebar.php etc. assets/ sf.css scal.css etc. ueber-uns/ index.php das-team/ sample.php There are a lot of different folders and sub-folders and I don't want to have different header.php files in all the different folders or duplicate the css as the menu/footer of this website is always the same. No 404 error or anything like that, I just included the code in the header.php file & removed the base tag & the css stopped working in index.php – Sophie Feb 07 '19 at 13:36
  • 1
    if you use the style tag it should give a 404 error in your console (on the webbrowser) if it can't find it. no matter where on the site you go the css files should always be located under localserver/kinderwunschpraxis-site/assets/sf.css so if you do `` does that work? i would check where the generated url is pointing per page alternative make your webserver point to only this project so that it is always the root. then you could just use `/assets/....` as this will always work from the root – mrQubeMaster Feb 07 '19 at 13:46
  • since you are new to this (and i had mayor trouble with this when i started) if you want to change where xampp points for `http://localhost` you can see [this](https://stackoverflow.com/questions/18902887/how-to-configuring-a-xampp-web-server-for-different-root-directory) post. you would go into `C:\xampp\apache\conf\httpd.conf` or go into the interface and hit config and select the file. you want to search for `DocumentRoot "C:/xampp/htdocs" ` in that file and change the value to where your site files are located – mrQubeMaster Feb 07 '19 at 14:06
  • Oh man... beginner problems indeed, sorry, I didn't even realise you were referring to the console. I just went back, removed the base tag and used in header.php, when I load index.php - there is indeed an error in the console: Failed to load resource: the server responded with a status of 404 (Not Found) – Sophie Feb 07 '19 at 14:09
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/188059/discussion-between-mrqubemaster-and-sophie). – mrQubeMaster Feb 07 '19 at 14:14