0

I have download a website that was written from scratch using PHP, MySql, html, css, jQuery.

After fixing some configuration with MySql and htaccess, I get to a point where all my CSS and JS files are not loaded because of a relative path error.

This is how it looks: enter image description here

The error in the console GET http://localhost/fonts/fonts.css sends me to index.php which loads all those files with a relative path. Now, if I remove the / at the beginning of each line, the files will be loaded, however, I get more errors on different pages with bad URLs and such, that means that I need to fix this / problem instead of just renaming the hrefs.

So my question is, how to fix it?

I have tried to set a base href tag:
<base href="http://localhost/israelrescue/">
or
<base href="http://localhost/israelrescue">
or in PHP:
$_SERVER['DOCUMENT_ROOT'] = 'http://localhost/israelrescue';
or
$_SERVER['DOCUMENT_ROOT'] = $_SERVER['DOCUMENT_ROOT'] . '/israelrescue/';

P.S:

israelrescue is the name of the folder I am using in my localhost installation for this website.

Nothing seems to work, any ideas?

Thanks!

odedta
  • 2,430
  • 4
  • 24
  • 51
  • send your live url – Mukesh Panchal Jan 23 '17 at 10:06
  • @MukeshPanchal what do you mean? it's installed on my laptop, locally, there is no live link. The website link is https://israelrescue.org/ if that helps... – odedta Jan 23 '17 at 10:07
  • 1
    It's because you've got the site in a subfolder in `localhost`. Is it possible for you to move the site into the root? I think @MukeshPanchal is telling you to load the assets from live, which is missing the point somewhat. – Tom Jan 23 '17 at 10:08
  • 2
    Looks like this site was not written to be run from within a sub-folder. Easiest way to work around that would be to set up a VirtualHost for this project in your local server, so that you can load it directly via a local domain name, without any additional path. (And btw., setting DOCUMENT_ROOT to an HTTP URL is complete nonsense.) – CBroe Jan 23 '17 at 10:09
  • It is possible, however, this site has tons of files and folders. I would like to avoid having a mess in my `www` folder because I am working on other sites at the same time. – odedta Jan 23 '17 at 10:09
  • @CBroe `setting DOCUMENT_ROOT to an HTTP URL is complete nonsense.` - I figured but wanted to try anyway. Could you explain how to get the VirtualHost solution working please? if that would be a solution you could write it as a solution post and I will accept if it works. :) – odedta Jan 23 '17 at 10:10
  • 2
    How to configure VirtualHosts is something you can research. Specifics depend on what web server you are using. – CBroe Jan 23 '17 at 10:12
  • 1
    please follow the link and make a virtual host in your computer if you use a windows machine http://stackoverflow.com/questions/2658173/setup-apache-virtualhost-windows – SarangaR Jan 23 '17 at 10:12
  • I understand, thank you for your replies! – odedta Jan 23 '17 at 10:19

2 Answers2

0

Answers:

It's because you've got the site in a subfolder in localhost. Is it possible for you to move the site into the root? I think @MukeshPanchal is telling you to load the assets from live, which is missing the point somewhat. – thebluefox

Looks like this site was not written to be run from within a sub-folder. Easiest way to work around that would be to set up a VirtualHost for this project in your local server, so that you can load it directly via a local domain name, without any additional path. (And btw., setting DOCUMENT_ROOT to an HTTP URL is complete nonsense.) – CBroe

odedta
  • 2,430
  • 4
  • 24
  • 51
0

You can leave the site in its subfolder as long as you drop the "/" in the beggining of every file path :

<link rel="stylesheet" href="/css/main.css">
<script type="text/javascript" src="/js/common.js"></script>

becomes

<link rel="stylesheet" href="css/main.css">
<script type="text/javascript" src="js/common.js"></script>

Should work like this

Mouradif
  • 2,666
  • 1
  • 20
  • 37
  • True, I already mentioned this in my original post. However, it wouldn't help me with the rest of the website because I only changed something locally in one file and one line. I needed a more general solution and VirtualHosts recommended by @CBroe worked well. – odedta Jan 23 '17 at 10:51
  • 1
    Oh sorry I didn't see that. Yes ! You can configure a virtualhost pointing directly to that folder under the ServerName "ir.localhost" and add a host entry "127.0.0.1 ir.localhost" in your `/etc/hosts` that way, your site would be at root level of that host. – Mouradif Jan 23 '17 at 11:02