I have a PHP file called header.php which contains as follows:
<title><?php echo $title; ?></title>
<link href="core/css/styles.css" rel="stylesheet" />
<link href="core/css/bootstrap.css" rel="stylesheet">
When on the root folder of my website it is easy to access these files. Simply using: include 'header.php'. However when I am in a directory other than root, for example order/ the files are not able to be accessed. I can access the PHP file by going back a directory using: include '../header.php' however this does not work for the css files included in that file and they cannot be accessed by the file that is not in the root directory.
Can anyone help me with a way to come over this? I like only having one header.php file as I can then add/remove files from that document and the change will appear across the whole site.
Many thanks,
Harry
When using a universal header it's important to use absolute paths to your css files, from the web root. So if core were a folder in the root this would be the revised header.php:
<title><?php echo $title; ?></title>
<link href="/core/css/styles.css" rel="stylesheet" />
<link href="/core/css/bootstrap.css" rel="stylesheet">
Notice the / at the beginning of the path denoting from the web root.
Related
i have started to add include files like header, nav, footer.php to my index.php in my root directory, i got a admin folder with another index.php, with the same include files except it is using "../" in its target path to go back one before accessing the includes file, what happens is it works except for the css files... i lose my styling, but on my root index.php if i go back to that, the styling is working.
Any idea why this is happening?
my css code is:
<link rel="stylesheet" href="css/main.css">
Use an absolute path in your link tag
<link rel="stylesheet" href="/css/main.css" />
As kindly pointed out by John, this is not an absolute path and in all honesty I have no idea what to call it (root relative?). What I do know is that it is relative to the root of the site and not the current folder on the server.
The syntax you're using is relative path.
Use either this to fix the relative path:
<link rel="stylesheet" href="../css/main.css">
Or make it absolute if your css directory is in the web docroot:
<link rel="stylesheet" href="/css/main.css">
if css folder is outside of your admin folder it should be ../css/main.css, right?
For a better management, I'll use an abolute path with full url
<link rel="stylesheet" href="{$html_css}/main.css" />
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 was wondering how was that possible.
Technically, I have a head.php file that contains all of the stylesheets and javascripts:
head.php
<!--CLIENT-->
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="../css/jquery.dataTables.min.css">
<link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css">
<script src="js/bootstrap.min.js"></script>
</head>
The file where the bootstrap.min.css is located is 2 folders up and I read somewhere that "../" is moving a step back. So I was wondering why it still works?
Here is the path information:
C:\xampp\htdocs\renewSAD
-includes folders client,css,fonts, & js.
inside the css folder are all the bootstrap css files.
`in the client folder
C:\xampp\htdocs\renewSAD\client
it includes another folder which is the head.php
Therefore it is 2 folders away for the css folder. How come a single ../ works in locating the path for bootstrap's css?
I asked this question because I think it might cause an error in the future.
If I understand the question correctly, you have the following structure:
C:\xampp\htdocs\renewSAD
- Client
- Another folder
- head.php
- CSS
- bootstrap.min.css
- ...
- Fonts
- JS
Any chance that your index.php is actually located in the Client folder, and head.php just gets included in that file?
If so, this works because paths are relative to the main script (not the included script). You can use getcwd() to verify the current directory.
My PHP files within folders are not rendering my default CSS files.
I have a "document-head.php" file that contains:
<link type="text/css" rel="stylesheet" href="assets/css/main.css"/>
Within each file I include this 'document-head.php';
For example, the directory I have is:
index.php
|__ assets
|__ css
|__ main.css
|__ includes
|__ document-head.php
|__ components
|__ secondary.php
|__ partials
Within "components/secondary.php" contains
<?php include('../_includes/document_head.php'); ?>
And within "index.php" contains:
<?php include('_includes/document_head.php'); ?>
How do I make sure that files within folders that include this "document_head.php" always render CSS/JS files?
Edit:
The reason I can't use absolute URL's is that one of the developers configured a script that allows me to upload my application to an FTP where I can name a new folder. For example, if all my files are in "secret-app", in the terminal I can publish my app to an FTP with the new folder "test-app" and it will create "mydomain.com/test-app/secret-app". (It's a ghetto system, but it's useful for my particular needs.)
Try change to an absolute href url like:
<link type="text/css" rel="stylesheet" href="/yourApp/assets/css/main.css"/>
If you're using a framework, you will have several ways to get your base_url.
if this stylesheet is within the document_head.php
<link type="text/css" rel="stylesheet" href="assets/css/main.css"/>
then try this :
<link type="text/css" rel="stylesheet" href="../assets/css/main.css"/>
sorry for my grammar :D
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.