Moving website to localhost: Document Root changed - php

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:
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!

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

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

Related

css file doesn't work on wahmpserver?

I've got a login form with css. When I open it from my whampserver (localhost) it doesn't display the css, but when I save it on my desktop and open it from their it does.
Does anyone have an idea why this happens.
pls don't down vote because it's obvious.
You're using a local absolute path to your CSS file ("C:\Users\marij_000....").
Once you'll deploy your site to a different server it won't be able to access your local computer , thus, the CSS link will be broken.
Best Practice advice. Add a "css" folder to your project and place all the CSS files in that folder, then use a relative path to your files. For example:
<link rel="stylesheet" type="text/css" href="/css/main.css">
the first "/" state that the server will start the path from the project's root folder.

header.php not displaying properly from root folder homepage

I have the index.php in my public_ root destination which has the header taken using php include from the folder include/header.php.
But when the index page loads the menu and images within the header.php doesnt load because its directory is from include folder.
Ex in header.php the menu will be in directory
<link rel="stylesheet" href="../scripts/menu.css" type="text/css" media="all" />
but the index.php directory to menu stylesheet would be scripts/menu.css. So i guess the menu styles are not loading.
Is there a way to get the index page right without having to have header in the same folder as index.php or is it because i am testing this on windows using xampp?
Let me clear this one up for you.
When you write HTML and CSS from PHP like you are doing here, the HTML and CSS don't know where the PHP is being run from or anything about the PHP. This is because PHP is interpreted and writes out the page into HTML which is then processed in your browser (i.e. the PHP never reaches your browser, all it sees is HTML).
This means that you must add the correct paths to the CSS files relative to your web server root not the PHP script location (in your case that is probably "/scripts/menu.css" for the example you've provided - assuming that the scripts is in the root web folder).
So for example, on your website / means the root of the web server (i.e. mywebsite.com/) and anything after that is it's path relative to the root of the web server.
By using /mycssfolder/mycssfile.css like I suggested, you can ensure that the location is correct regardless of the page which the PHP script is called on.
Hope this helps.
Ben
<link rel="stylesheet" href="/scripts/menu.css" type="text/css" media="all" />
You might try simple removing the .., like shown above. Remember, any external source file that you include becomes part of the destination. Thus, think of the location of your CSS file relative to index.php.

Hosted Site Not using CSS/JQuery

I have created a site, running it locally using XAMPP as i developed it, and it runs perfectly when local.
However i tested it when hosted and the CSS & JQuery (Google library) do not work at all.
The CSS tags use the standard:
<link rel="stylesheet" type="text/css" href="/css/css.css">
Is there a reason this wouldnt work when live? But surely this has nothing to do with why JQuery doesnt work either?
I dont feel comfortable leaving it live when it doesnt work, so im sorry i cannot give you a live version to view.
Any help would be great!
Do check with Inspector if the CSS and JS files are loaded correctly when you go live — do note that the first slash in front of the URL means you're referencing the root URL of the live site. Are the directory structures of the local and live site different?
Also, you can try loading the external files using the absolute URL, like http://domain.tld/css/css.css (double check if the file exists, or loadable). If it works, then it's your relative URL path that is not working.
[Edit]: For the sake of completeness, this is how relative URL paths work:
/<fileName>.<fileExtension> will point to the file at the root directory
./<fileName>.<fileExtension> will point to the file in the same directory as the current page
../<fileName>.<fileExtension> will point to the file in the directory one level above, relative to the current directory
Check that you have actually uploded CSS files into /css/ folder. (Obviously /css/ folder should be present on your server)
Make sure you can access you CSS files from the web: eg if you go herehttp://yourdomain.com/css/css.ss you should see the contents of you CSS file.
if you can see the contents of your file here: http://yourdomain.com/css/css.css
then try this:
<link rel="stylesheet" type="text/css" href="http://yourdomain.com/css/css.css">
And as #Terry already mentioned check the paths.

linking to css in localhost getting lost in multiple pages

I just recently installed XAMPP and have been trying to get my former website which was hosted online to work in it.
Everythings okay until I navigate past my baseurl. To make matters a bit more complicated, i've been the page using jquery-mobile so all the content is fetched through AJAX.
baseurl = 127.0.0.1/mobilebrowsergame/
When I refresh(start) on this page, everythings okay because it uses the default root for the CSS files.
However, if I start/refresh on a page a bit deeper, ie.
127.0.0.1/mobilebrowsergame/structure/1
The css obviously doesnt work.
I originally included <?php echo base_url() ?>css/stylesheet.css ?> but was forced to remove the base_url() because it wasn't loading under localhost.
At this point, I'm at a loss. I don't know how to tackle this without creating a new CSS for ever page under the base.
Any ideas?
It looks like you are pointing to your CSS files using a relative URL. If you prepend a path with /, it will automatically start at the web serve root (in this case, 127.0.0.1), instead of at the current directory.
i.e. Try this
<link rel="stylesheet" type="text/css" href="/mobilebrowsergame/css/stylesheet.css" />

Relative path in a php site on a windows server

I have a PHP enabled windows server, where i have to get a site working.
Its all well and good, the php is working, afaik. But for some reason i can not get relative paths to work in the page header.
I have this folder structure(only relevant folders):
wwwroot/Sitename/
wwwroot/Sitename/Control
wwwroot/Sitename/Images
wwwroot/Sitename/Model
wwwroot/Sitename/View
In IIS i have set the the physical path to wwwroot/Sitename/Control. This loads pages.php(which is located here) which in turn loads wwwroot/Sitename/view/header.php and footer.php.
This works perfectly, however in header.php there are some references to some javascript (and other) files located in wwwroot/Sitename/Model eg.:wwwroot/Sitename/model/js.js
(eg:
<link rel="stylesheet" type="text/css" href="../model/main.css" />
)
This file is not found by the browser when loaded. I am sure it has something to do with the fact that i have set the physical path as i have but this is needed to load pages.php.
Can anyone point me in the right direction? either why the relative path is not working or perhaps another way to set the physical path/something similar.
EDIT: clarifications in parenthesis
use ../ to go down a folder.
Try this in your header.php:
../Model
Why dont you use Absolute urls?
$SiteURL = 'http://www.your_iis_sever.com';
Then in your javascript files use
<?php echo $SiteURL;?>/path/to/js/jscript.js
infront of the links. Make sure its all correct => Check the source code when viewing the page if the paths are correct. From there its easier to navigate.

Categories