This is how my project is currently set up:
-index.php
-css
--index.css
-includes
--head.php
head.php
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="../css/index.css">
<title> My Website </title>
</head>
<body>
index.php
<?php
include(__DIR__.'/includes/head.php');
?>
The problem is that index.css does not work. I don't see what I'm doing wrong at this point. Any help would be greatly appreciated. Thanks.
The problem is in the way PHP include() works; it literally includes the code from head.php into index.php, without updating any relative links. As such, you index.php is looking for the CSS file in ../css/index.css. And while your head.php does indeed need to go up one directory before looking in the CSS folder, your index.php file does not.
To resolve this, you have a number of options:
You can update the relative path to css/index.css to work from your index.php file.
You can use the root-relative path /css/index.css to reference the CSS file from any folder.
You can use the absolute path https://yourwebsite.com/css/index.css to reference the CSS file from not only your website, but any other website. This further eliminates confusion, but will cause issues if you change the domain.
Personally I would recommend the root-relative path, as it makes things less confusing when using things like includes and partials.
Related
I would be grateful if someone could help me with this since i have been going about this for a long time without really understanding what to do when using the .htaccess script for user friendly urls. So the main issue is; the css script in a subdirectory is not accessed by a php script whose main root directory is a subdirectory in the htdocs(web) directory.
Below is the structure of my webdirectory:
I have php website (in development) which I has following structure
htdocs
+ myweb.com
+ assets
+ css
+ style.css
+ includes
+ config.php
+ admin
+ home.php
+ home.php
PROBLEM
I am including css in /home.php as ..
<head>
<link href="assets/css/styles.css" rel="stylesheet" />
</head>
above is working. Now I wanted same link to be working in /admin/home.php
<head>
<link href="assets/css/styles.css" rel="stylesheet" /> <!-- Not working -->
</head> <!-- This wanted -->
<head>
<link href="../assets/css/styles.css" rel="stylesheet" /> <!-- working -->
</head>
I want a mechanism where file url should start from base. like if I set base to myweb.com, then css inclusion should be valid for /assets/css/style.css in any of file irrespective of location. I tried base rewrite but that is not helping
I tried in .htaccess file
RewriteBase /myweb.com/
DirectoryIndex home.php
Platform : XAMPP for windows 64bit..
Thank you in advance for your kind consideration.
If your URL doesn't start with https:// or / then it is considered relative to the document calling it. So, from a file in the admin directory, assets/css/styles.css will look for a directory called assets and won't find one.
If you want to ensure URLs are always resolved relative to the root, include the root in the URL like so:
<link href="/assets/css/styles.css" rel="stylesheet" />
This will work from anywhere, because it is an absolute path.
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.
From a PHP script, I want to include a "header.php" and "footer.php" file, which are in a different directory. They have HTML in them, and the HTML has references to file paths in them such as:
<link rel="stylesheet" type="text/css" href="css/custom.css">
and
About Us
When I include this html from the php file (which is in another directory) the html looks for the file paths in the directory it's included into, instead of where it's included from.
Is there an easy fix to that? I realize I could go through the html and prepend something like <?php echo dirname(__FILE__); ?> into every file path. But is that the correct way to do that?
Should I restructure my files completely?
Currently the reason the php script is in a different directory is because currently most of my web pages are in the root directory of the server -- so my css js and template files are in folders named "css" "js" and "inc"; but I want to have some web pages be in a url of the format "[root]/Account/settings.php". So I'm trying to put some php pages into a folder in the root directory named Account. But when I try to include my templating files, the file paths in them get messed up.
Use the following path -
<link rel="stylesheet" type="text/css" href="/css/custom.css">
and
About Us
This is assuming that the CSS directory and About Us are both in your document root. Really a simple HTML and path issue rather than PHP, if I am understanding what you are trying to do.
I have the following file structure on my PHP localhost:
Root
css/
main.css
admin/
admin.php
index.php
header.php
Both admin.php and index.php are requiring header.php, in which main.css is included like the following:
<link rel="stylesheet" type="text/css"
href="<?php echo $_SERVER['DOCUMENT_ROOT']; ?>/css/main.css" />
However, the css file isn't included properly. When I look at the Source Code in Chrome on Mac, The browser has turned that into
http://localhost/Users/ljhljh235/Documents/web/hetd/css/main.css
in which http://localhost is not intended to be here. Could anybody help me on how to build the correct absolute path for the css file?
P.S. My way of including header.php in admin.php and index.php is
require_once ($_SERVER['DOCUMENT_ROOT'].'/header.php');
And I'm using MAMP 2.1.1.
Thanks for any help.
The variable $_SERVER['DOCUMENT_ROOT'] is for server side path, use /css/main.css only, it will be relative to the domain ie : http://domain.com/css/main.css
I'm trying to set up my site in Dreamweaver CS5 to work with my local server, and I'm having issues with document relative links.
I've got a structure on my HD like this
website
_Common
header.php
_css
twoColFixRtHdr.css
index.php
and the same structure mirrored on my local WAMP server, except on the local server the site is in a subfolder, so it's something like www/website/
The problem is this line inside header.php
<link href="../_css/twoColFixRtHdr.css" rel="stylesheet" type="text/css" />
That looks correct to me pathwise, but on the local server it cannot find that css file from the header.php
If I change it to
<link href="/website/_css/twoColFixRtHdr.css" rel="stylesheet" type="text/css" />
or
<link href="_css/twoColFixRtHdr.css" rel="stylesheet" type="text/css" />
It works fine, but I want to use document relative links if I can, any ideas?
This sounds clear to me, since I'm sure that you include the header file in the index.php file. So the path for the css files is set relatively to the index.php.
you may achieve it by several ways.
one of them: you can setup a VirtualHost (in httpd.conf) and point your subfolder as root folder for host.
If header.php is being included in index.php, the relative link ../_css/ in header.php won't work because index.php is at the same level as _css.
For all intents and purposes, once it's been included in index.php, to the browser, the content in header.php is now simply part of index.php, so all paths need to be relative to index.php.
Ie:
index.php
/my_include_folder
- header.php
/_css
- style.css
Once I add <?php include('my_include_folder/header.php); ?> to index.php, the links to css files, js, and hyperlinks in header.php should be relative to index.php.
Hopefully that makes sense.