How use absolute link in href header - php

I work on my website but I have a problem with the links in the href.
My CSS and javascript files are in this folder (in the root of my site):
assets/stylesheets/file_css.css
For now the links of my href looks like this:
<link rel="stylesheet" href="./assets/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="./assets/fonts/font-awesome/css/font-awesome.min.css">
<link rel="stylesheet" href="./assets/stylesheets/custom.css">
<link rel="stylesheet" href="./assets/stylesheets/media-queries.css">
I call it in all my php files with :
require_once __DIR__.'/layouts/header.php'
Problem:
When I want to create a folder to sort my files eg views (folder found at the root of my site)
Looks like views/test.php
All href don't work, console error :
test.php:10 GET http://localhost/mywebsite/views/assets/bootstrap/css/bootstrap.min.css
test.php:11 GET http://localhost/mywebsite/views/assets/fonts/font-awesome/css/font-awesome.min.css
test.php:12 GET http://localhost/mywebsite/views/assets/stylesheets/custom.css
In fact, my CSS aren't in views folder.
I tried many things
1 - <link rel="stylesheet" href="/assets/bootstrap/css/bootstrap.min.css">
error => test.php:10 GET http://localhost/assets/bootstrap/css/bootstrap.min.css
2 - <link rel="stylesheet" href="<?php echo __DIR__ ?>/assets/bootstrap/css/bootstrap.min.css">
error => test.php:10 Not allowed to load local resource: file:///C:/wamp/www/mywebsite/layouts/assets/bootstrap/css/bootstrap.min.css
I thought to put href="/mywebsite/assets/bootstrap/css/bootstrap.min.css" in the first link, it works locally but not on the server
I really need help, I start having too much PHP file at the root
Thank you

Related

bootstrap.min.css Failed to load resource: net::ERR_CONNECTION_REFUSED

I am struggling with linking all my files : css, JavaScript, bootstrap, images etc... On my local server works everything fine. When I upload to my web server I got this error...and I know it is because of the path that I have. It searches everything on my local. and I am using Codeigniter framework, so I have no idea what the path should be when I put it online.
Example :
<link rel="stylesheet" type="text/css" href="<?php echo css_folder_url('reset.css'); ?>" media="screen"/>
<link href="<?php echo base_url() ?>web/bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
This is how I need to link in this framework
Maybe I should change something in my host file :
andreaportfolio.local
::1 andreaportfolio.local --> maybe should be .com instead of local
Photo about the error message
here there is how things should work in a small step-to-step guide:
Create a folder called 'assets' in /wamp/www/yourProjectDirectory [In WAMP server];
Edit the config.php in yourProjectDirectory/application/config directory as:
$config['base_url'] = 'http://localhost/yourProjectDirectory/';
<link href="<?php echo base_url('assets/css/bootstrap.min.css'); ?>" rel="stylesheet">
Create the controller as Boots.php;
Create the view file as samplebootstrap.css
<link href="<?php echo base_url('assets/css/bootstrap.css');?>" rel="stylesheet">
Go to the web browser and type:
http://localhost/yourProjectDirectory/index.php/boots/getBoots
Here you can find a dedicated thread on the topic:
https://forum.codeigniter.com/thread-1146.html
Hope this could help,
L.
This error usually means that the specific file was not found. So either the path is wrong or the file is not present where you want it to be. Try to access it by entering source address in your browser to check if it really exists there. Browse the directories on the server to ensure that the path is correct. You may even copy and paste the relative path to be certain it is correct.
There can be a '/' missing as well, for example:
<link href="<?php echo base_url() ?>web/bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
could be
<link href="<?php echo base_url() ?>/web/bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">

Css file not linking with html/php file

I have looked over the code and cant seem to see where the link between the two files is failing. My index page uses the exact same code to link the files and it works fine. When I add the styling in the header of the php/html file it works but otherwise nothing happens.
The reference that you have for the stylesheet is relative to the url of the page you're on.
For example, this:
<link rel="stylesheet" type="text/css" href="style.css">
... says that the stylesheet is at the same path that the page is on. So if the url in the address bar is https://somewebsite/admin/index.php then that page will try to load the stylesheet at https://somewebsite/admin/style.css. If the url is https://somewebsite/index.php then the page will try to load the stylesheet at https://somewebsite/style.css.
If the stylesheet resides at the root of the site then your link tag should look like this:
<link rel="stylesheet" type="text/css" href="/style.css">
This will make your page look for the stylesheet at https://somewebsite/style.css regardless of what the path of the url says.
You should make the right path, right into your css file at
Example:
<link href="assets/css/blabla.css" rel="stylesheet">

Moving back 2 directories not working?

Hello I am trying to move back 2 directories.
For example i have a file in my root folder ie: /root/FILE that links this stylesheet successfully with the file location being /root/blog/wp-content/themes/dazzling/inc/css/CSS-FILE.css"
<link href="blog/wp-content/themes/dazzling/inc/css/font-awesome.min.css" rel="stylesheet" type="text/css">
Now I want to link in the same stylesheet in a file located in a folder that is /root/eliquid/includes/FILE and in there and I am attempting to use
<link href="../../blog/wp-content/themes/dazzling/inc/css/font-awesome.min.css" rel="stylesheet" type="text/css">
I also attempted
<link href="../..blog/wp-content/themes/dazzling/inc/css/font-awesome.min.css" rel="stylesheet" type="text/css">
But not sure what im doing wrong! Any help is much appreciated!
Have you tried using an absolute link rather than a relative link? If your CSS file is at http://www.example.com/blog/wp-content/themes/dazzling/inc/css/font-awesome.min.css then you can include it on any page of your website as
<link href="/blog/wp-content/themes/dazzling/inc/css/font-awesome.min.css">

PHP - Including HTML from different folder

I am currently working on a admin backend and now want to move some pages into different folders (so I do not have all scripts in one folder).
Structure looks like this:
/
/theme/assets/...
/templates/head.php
/templates/foot.php
/top/index.php
/index.php
In head.php there are some stylesheets added like this
<link href="theme/assets/global/plugins/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
This works fine for files in the root folder (e.g. index.php), but when I use the same head.php in /top/index.php it does not work because the path is wrong.
Is it somehow possible to properly include the stylesheets etc. while having the scripts in different folders?
Greetz,
Just specify the root identifier (/) at the beginning of the href attribute:
<link href="/theme/assets/global/plugins/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css" />
Try this,
<link href="../theme/assets/global/plugins/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
In /top/index.php
OR
<link href="<?php echo $_SERVER['DOCUMENT_ROOT'] ?>/theme/assets/global/plugins/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css" />
This code can be used in any page. The $_SERVER['DOCUMENT_ROOT'] points to the root.
Use '../' to reference the folder above and work your way back.
e.g
<link href="../theme/assets/global/plugins/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">

PHP Website - Loading Stylesheets in Sub Directories

I'm building a PHP based site with this directory structure
index.php
css
style.css
bootstrap.css
includes
header.php
footer.php
bikes
road.php
mountain.php
The Problem
So I'm working on road.php and I obviously need to be able to link to both style.css and bootstrap.css, but when I declare at the start of road.php to include the header.php and footer.php it is like as if it cannot find the stylesheets and the site reverts back to the default 1990s look.
I have also found that any form of link on the page loads a 404. I'm only just starting out with PHP because I need some more power in my sites, but I just can't seem to get my head around the super basic things.
I just don't know what to do and I'm finding myself turning my back on the whole PHP language.
Thanking you in advance,
Stu :)
I can't be certain without seeing the actual content of header.php (in perticular the part where you import the stylesheets), but it sounds like you are using a relative path to your stylesheets. Something like <link rel="stylesheet" type="text/css" href="css/style.css" media="screen" />. This works fine for index.php, but since the other pages are inside the subfolder bikes, they will be looking for the CSS files in yoursite.com/bikes/css.
The solution is to provide an absolute path. Something like this:
<link rel="stylesheet" type="text/css" href="http://yoursite.com/css/style.css" media="screen" />
This way, it doesn't matter if the page is inside a subfolder (or a subfolder of a subfolder) - it will allways look for the CSS file in the right location.
If you are using multiple domain names, or for some other reason you cannot hardcode the domain name, you can prepend a slash (/) to the path as well:
<link rel="stylesheet" type="text/css" href="/css/style.css" media="screen" />
This path is relative to the root of the website, not to the current directory.

Categories