URL rewrite with include does not load stylesheet - php

All my files are in the public_html folder. I rewrote the url of the pages with .htaccess, so e.g the url of mywebsite.com/balance.php looks mywebsite.com/myaccount/balance.
I can include a file with: <?php include 'header_login.php'; ?>, but it appears without the stylesheet.
Both this .php file and the .css file are in the public_html folder.
If I rewrite the url to mywebsite.com/balance it works.
How can I make this work with this "virtual" folder in the url?

Simple.
Always use absolute paths in you HTML and CSS files.
An absolute path always starting from / pointing to the web-server root.
So, make your css path like
/css/styles.css
or whatever.

The address of the stylesheet is possibly not correct. You have to use absolute paths.
If your structure is like this:
/balance.php
/style.css
In your balance.php you use: <link rel="stylesheet" href="style.css">
And rewrite it to: /myaccount/balance
The browser will look for a style.css file at /myaccount/balance/style.css .
Just change it to an absolute path and you will be fine.

You can define your base url
define ("BASE_URL","http://www.mysite.com");
and add BASE_URL with stylesheet like
<link rel="stylesheet" type="text/css" href="<?php echo BASE_URL; ?>/style.css">

Related

File don't get attached when I use absolute path in HTML CSS

I was using PHP include function to include the header and footer but now I want to change location of my files so I created a folder named admin panel and created a PHP file in it. Now I want to connect my header.php file using include but all the path in header file are give in reference so I want to change it to absolute path but when I change it then it doesn't work.
The reference file path is given like this:
<link href="css/bootstrap.css" rel="stylesheet" type="text/css" media="all">
Now when I change it to absolute then it dont work
<link href="/home/username/public_html/css/bootstrap.css" rel="stylesheet" type="text/css" media="all">
I am having GoDaddy hosting so this is the way to add absolute path in GoDaddy they say. This is GoDaddy's answer link:
https://in.godaddy.com/help/what-is-my-absolute-path-16023
If I am doing something wrong then please tell me thanks in advance.
If you use a forward slash at the beginning of your path, the path will start in your root folder.
Like this:
<link href="/css/bootstrap.css" rel="stylesheet" type="text/css" media="all">
This path should work in any file in any directory.
Best way for you is to get the absolute path through php, so you have no doubt. And to get that just use this
<?php
$path = getcwd();
echo "The absolute Path is ".$path;
?>
Suppose you site is www.example.com and you are giving the absolute path like /home/username/public_html/css/bootstrap.css it will search at http://www.example.com/home/username/public_html/css/bootstrap.css but if you give like /css/bootstrap.css it will search at http://www.example.com/css/bootstrap.css. if there is a forward slash at the beginning.

php include file from another directory

Here is a structure example:
/main
/css
style.css
/include
article1.php
article2.php
header.php
index.php
In my header.php I have the following code for the css:
<link rel="stylesheet" type="text/css" href="css/style.css" />
And, for example, in my index.php I have the following as the first line:
<?php include 'header.php'; ?>
Now, everything works fine as it is. Next I'm going to insert the following code in the article1.php file:
<?php include '../header.php'; ?>
The contents (menus and other html) are displayed correctly, however the CSS won't be displayed/recognized at all. Basically what's happening is that the header file is being included but the server isn't respecting the directory parenting. For the CSS to be displayed correctly I'd have to change the link rel for the CSS to ../css/style.css, but if I do so it won't work on files located in the main directory.
I hope I made my problem clear. What am I doing wrong? How can I include files from different directories and preserve the links inside them?
In your site's <head> section, insert a <base> element with the href attribute. Set the href attribute to the base URL of your website, and all relative requests will be sent through that base URL.
<base href="http://my-website-url.com/" />
<link rel="stylesheet" type="text/css" href="css/style.css" />
With a correctly-set base tag, the user's browser will attempt to load your stylesheet from the URL http://my-website-url.com/css/style.css.
Note: this not only affects stylesheets, but all relative links in the document.
It has to do with how pathing works in includes. I recommend pathing things from the doc root whenever possible.
<?php include( $_SERVER['DOCUMENT_ROOT'] . '/header.php' ); ?>
That will work in any of your files.
Instead of using relative paths:
href="css/style.css"
use absolute paths from the webroot:
href="/css/style.css"
You should include your css file from the root. So /css/style.css so way it will always start at the root and then go down from there. I believe that should fix your problem in all cases.
First off, the problem is that your link to your CSS files is wrong. The best thing to do is look at the output of the HTML (view source) from the page. So lets break it down (form index.php):
Index.php is located at domain.tld/index.php. Your css files are located at domain.tld/css/*. When viewing the index file, your header is
<link rel="stylesheet" type="text/css" href="css/style.css" />
which works because you are at the top of the directory (or root) and the links are made relative to this directory. Now when you go up to domain.tld/include/article1.php, you are no longer at the root. It is trying to access:
domain.tld/include/css/style.css
This means you have to build the full link or change your relative. The easy way since the CSS is at the root is just to use the following
<link rel="stylesheet" type="text/css" href="/css/style.css" />
^
This means it will look at the root of the domain for the css directory as such domain.tld/css/styles.css. If your top directory is /main, use /main/css/styles.css
Using relative includes from filed already called as includes is always a problem, each time you specify a relative location, replace your
../some_directory/some_file.php
with
dirname(dirname(__FILE__)) . '/some_directory/some_file.php'
dirname(__FILE__) is the current directory.
May I explain your problem and how to solve it.
An include in php works as in C.
When you include a page that copy/paste content of the page into the first one.
So, there are two files:
include.php
<?php
$var = 'PHP';
?>
main.php
<?php
include 'include.php';
echo $var; // affiche 'PHP'
?>
When you request for "main.php" page you will get the following:
<?php
$var = 'PHP';
echo $var; // affiche 'PHP'
?>
So if you need to include an element to your page. You have two choices:
1. absolute path (better way)
Instead of using relative path, use absolute path. This way allows you to include a file from everywhere without of use of the current path.
2. variable
You can use a variable containing the relative path to the root directory of your repository/website. So each time you have to include a page or create one you have to define a variable as following:
include.php
<?php
$var = 'PHP';
echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"".$currentPath."css/style.css\" />";
?>
main.php
<?php
# path to the root of the website
$currentPath = "../";
include 'include.php';
echo $var; // affiche 'PHP'
?>
To learn more about include, see this page
You have two options
Pass the relative path to the included file
ie.
<link rel="stylesheet" type="text/css" href="<?=$path ?>css/style.css" />
and
$path = "../";
include "header.php";
or 2. you use absolute paths to your files
<link rel="stylesheet" type="text/css" href="/css/style.css" />

php include with html css

I have a php file that i include in other php files. This php file is the menu. It links to my stylesheet with <link href="styles/main.css" rel="stylesheet/index" type="text/css" />. But this only works if the php file that includes it is correct according to the path. How can I use absolute paths?
Say if I include the menu.php from another folder how can you automatically update the path to the css file?
Just use the absolute path, href="/style/main.css"
<link href="/styles/main.css" rel="stylesheet/index" type="text/css" />
It means styles/main.css from root of website
An absolute URI is like this:
href="http://example.com/absolute/path/to/file.css"
an URI relative to the current directory is like this:
href="relative/url/to/file.css"
an URI relative to your site's root (http://example.com/) starts with a /:
href="/relative/path/from/yoursite/to/file.css"
Inside your css file all URLS are relative to it's own position so it's behaviour doesn't change when the path of the file which includes it changes.
In example if your menu.css file is located into
http://example.com/styles/menu.css
just use
<link href="/styles/menu.css" rel="stylesheet/index" type="text/css" />
And browsers will always look for menu.css in http://yoursite.com/styles/menu.css
Use the full path relative to your document root instead of the relative path. E.g.
<link href="/styles/main.css" rel="stylesheet/index" type="text/css" />
as you suggested, just you absolute paths. So when you link to your stylesheet, just use the the full path like http://www.mysite.com/css/myCssFile.css
By the way always make sure you put a base tag on your HTML, like that:
<base href="http://www.mysite.com/" />

Smarty Relative include path

File structure is as follows:
index.php
settings/
|-manage_account.php
templates/viriditio-v2/
|-index.tpl
templates/virditio-v2/css
|-style.css
localhost/~braden/virditio/index.php shows the template like expected showing index.tpl with the style sheet paths correctly showing:
<link rel="stylesheet" href="templates/virditio-v2/css/style.css" type="text/css"/>
However localhost/~braden/virditio/settings/manage_account.php shows the same path, which is the relative path (should be ../ to be complete).
What's an easy way to make it relative to the template? Or absolute to the root? Is there a Smarty function that includes css files and makes them not relative?
Currently I have it set like this:
config:
template_url = "templates/virditio-v2/"
and .tpl:
<link rel="stylesheet" href="{#template_url#}css/reset.css" type="text/css"/>
EDIT
It's not pretty but I was able to accomplish it with
{assign var='config_url' value=#template_url#}
{assign var='template_url' value=http://`$smarty.server.SERVER_NAME`$config_url}
Any better solutions out there?
Why not access the CSS like
/templates/virditio-v2/css/style.css
with an absolute path?
If its about different hosting environments in subdirectorys, consider a config option to set the base directory and append it as a vairable to the path in your smarty template.
If I understood correctly the issue is to have a CSS file linked to a webpage no matter where in the folder tree the page comes from. This might be written as:
<link rel="stylesheet" href="/~braden/virditio/templates/virditio-v2/css/style.css" type="text/css"/>
This would make it a fixed path starting from the root of the server.

set include_path for embed files like .js and .css?

OK so I now know there is a way to put all my php files in a single place and have them be able to be included without a filepath by setting an include_path like so:
php_value include_path .:/pathToPHPFiles
OK so now as long as my PHP files are in this directory I can include them from any subdirectory as if they were in the same directory.
I am wondering if there is a trick like this for other file types like .css and .js so I can put them all in single location and embed them in a page without worring about the filepath?
define("CSSPATH", "/path/to/css/");
echo '<link rel="stylesheet" type="text/css" href="'.CSSPATH.'style.css" />';
or, in a .htaccess file:
RewriteEngine on
RewriteRule ^(.*\.css)$ path/to/css/$1
I'm not sure about the .htaccess version... corrections welcome.
For this you can use the base tag.
...
<head>
<base href="/somepath/">
...
</head>
<body>
<!-- the following images src will be resolved into '/somepath/image.png'
<img src="image.png" alt=""/>
...
But then again, why not just use absolute urls, that is, urls beginning with / ?

Categories