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 / ?
Related
I'm coding something into CodeIgniter and I want to apply a css template. I found one I like and download it.
After merging the css code with the code I have before the merging, I found that the images used by the css template doesn't load. Originally they should stay on root of html folder (or www, or public_html, you know what I mean...), but I put them under an assets folder which is on same level as system folder.
Something like this...
Website Folder
|
|---application\
|-----(CodeIgniter application folders...)
|---assets\
|-----style.css
|-----css\
|-------mini.css
|-----images\
|-----js\
|-----robots.txt
|---system
|-----(CodeIgniter system folder...)
|index.php
I googled for a couple of hours and I found this post (post #5). I try what the OP says but it doesn't work.
I can autoload the url_helper adding
$autoload['helper'] = array('url');
to the autoload.php file. But when I add
<base href="<?php echo base_url() ?>"/>
the images are still absent.
The original html file has this line
<link href="style.css" type="text/css" rel="stylesheet" />
so, I'm guessing that I should add assets/ so it looks
<link href="assets/style.css" type="text/css" rel="stylesheet" />
but still the images are missing.
If I move the contents of assets folder to root, everything is fine, but of course, that's not a good practice AFAIK...
My base_url is
http://localhost/prog/nonsense/mvc/
Before you ask, yes, I did read the .htacces solutions, but I really don't want to mess with .htaccess editing for now.
A little help here could be appreciated.
have you check .htaccess ?
It must have something like:
RewriteCond $1 !^(index\.php|assets|upload|robots\.txt|.*\.css)
I have the same thing as you, and I call them like this:
<link href="<?=base_url('assets/style.css');?>" rel="stylesheet">
This works for me if the base_url() is set and the url helper is called.
I call the base_url() and then the assets folder then style.css. There's no need for a / after base_url();?> because there's one in the base_url() anyway.
Another option using codeigniter's built in HTML function:
Also, if you look at this link, you'll see you can use the HTML helper and call it via codeigniter's built in functions.
This time, you shouldn't need base_url();
do:
$autoload['helper'] = array('url', 'html');
then in the view for header add this:
<?=link_tag('assets/style.css');?>
It will output to this:
<link href="http://localhost/prog/nonsense/mvc/assets/style.css" rel="stylesheet" type="text/css" />
base_url could also be used as link_tag:
<link href="<?php echo base_url('assets/style.css'); ?>" rel="stylesheet" type="text/css" />
would work too and keep your code cleaner.
yes! I just added the following line in .htacces file it working fine
RewriteCond $1 !^(index\.php|images|robots\.txt)
I just add one line in .htacces side of application folder
RewriteCond $1 !^(index\.php|images|robots\.txt)
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">
I am using following RewriteRules :
RewriteRule ^([a-zA-Z0-9]+)$ page.php?p=$1
RewriteRule ^e/([a-zA-Z0-9]+)$ edit.php?p=$1
The first one works fine : when typing mysite.com/id it loads mysite.com/page.php?p=id on server side.
The second one is working as well : when I type mysite.com/e/id it loads mysite.com/edit.php?p=id (as expected). But in that case edit.php can't loacte any external files like my css file.
<link rel="stylesheet" type="text/css" href="style.css" />
When doing either :
<link rel="stylesheet" type="text/css" href="../style.css" />
Or simply removing the direcory in the RewriteRule like :
RewriteRule ^e_([a-zA-Z0-9]+)$ edit.php?p=$1
it fixes that problem.
Now I don't understand why my edit.php is unable to locate external files even thought it loads on the correct path on server-side (mysite.com/) and not like the url displays in an extra directory (in this case mysite.com/e/ ).
You can use a base url for all the site's paths, like this:
<base href="http://yoursite.com/blog/" />
or
<base href="/blog/" />
And then writing relative paths from the base's:
...
Plus, you have to be extra careful with those "accept all" patterns, as they may conflict with the stylesheets, images and the rest of media if not used properly. Either appending an extension to the patterns or excluding media could be possible fixes.
you have to write full path not the relative path when using mod_rewite
References
apache mod rewrite
one more
beginners guide
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/" />
I have a site with index.php in the root folder, images in /img , and overview.php in /content. I have a sidebar.php file that is included in both index.php and overview.php. How should I refer to /img/image.gif if I include a link in each file?
The location of image.gif changes relative to the location of the file that references it.
Using /img/image.gif in sidebar.php will work in index.php, but it fails for the file located at /content/overview.php.
The only solution that I can see is to either include a separate sidebar.php in each sub-directory, or include an /img directory in every sub-directory.
The best suggestion that I can find is to use the <base> HTML tag as suggested here:
Change relative link paths for included content in PHP
However, in the same link, SamGoody suggests that the <base> tag is no longer properly supported in Internet Explorer, since version 7.
I'd like some insight on the matter before committing to a course of action.
Thanks.
EDIT: I am using the wrong approach below with "../"
Example-
root/index.php:
...
<link rel="stylesheet" type="text/css" href="style.css" />
<title>title</title>
</head>
<body>
<?php include('include/header.php'); ?>
<?php include('include/menu.php'); ?>
...
root/include/header.php:
...
<div id="header">
<span class="fl"><img src="img/dun1.png"/></span><span class="fr"><img src="img/dun2.png"/></span>
...
root/content/overview.php:
...
<link rel="stylesheet" type="text/css" href="../style.css" media="screen" />
<title>Overview</title>
</head>
<body>
<?php include('../include/header.php'); ?>
<?php include('../include/menu.php'); ?>
...
Using /img/image.gif in sidebar.php will work in index.php, but it fails for the file located at /content/overview.php
But it shouldn't. The preceding / makes it an absolute path which will work from any point on the server. If this doesn't work for you, there's a problem somewhere - in that case, post some examples.
Unless you are planning to move the whole site into a sub-directory one day, or move images to a Content Delivery Network (both actions would require re-writing the addresses) you can safely use absolute URLs.