How to link to files and change base url in CodeIgniter? - php

Hello I've been running into a few issues with CI lately. When I want to link a css file to pretty much anything, I need to put it directly into the root folder. If I move the file somewhere further in the hierarchy it won't load it. Here's an example of when I am trying to link this core_css.css. It loads the one in the htdocs folder, but not the one in the css folder.
Works:
<link href="<?php echo base_url(); ?>core_css.css" type="text/css" rel="stylesheet" />
Does NOT work:
<link href="<?php echo base_url(); ?>application/OBS/css/core_css.css" type="text/css" rel="stylesheet" />
I've tried putting the css filed in all the other subfolders and linking those, but it only loads the one in the root.
My second problem is that every time I try to test a controller, I need to access it through index.php like this:
http://localhost:8888/index.php/test_controller
Is there a way to get rid of the need to put the index.php in the URL?

For your first problem:
Add .htaccess file in your root web directory and write allow from all it means, in this folder your all files and all folder will not give error Access forbidden! use it like this:
<link href="<?php echo base_url(); ?>application/public/css/style.css" rel="stylesheet" type="text/css" />
N.B:
I usually put all my files like that into an "assets" folder in the application root, and then I make sure to use an Asset_Helper to point to those files for me. This is what CodeIgniter suggests.
For your second problem:
If you are using Apache place a .htaccess file in your root web directory containing the following:
RewriteEngine on
RewriteCond $1 !^(index\.php|[Javascript / CSS / Image root Folder name(s)]|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
for better understand you can see codeigniter URL section through this link
http://ellislab.com/codeigniter%20/user-guide/general/urls.html
and another one is http://snipplr.com/view/5966/codeigniter-htaccess/

For the second question: put the following in an .htaccess file inside your CodeIgniter root folder (htdocs in your case):
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?/$1 [L,QSA]
Make sure to set $config['index_page'] to an empty string, and also set $config['base_url'] accordingly (these two options are inside /application/config/config.php)
For the first question: try to make a folder called assets, or something similar and put it in the root of the site (again inside htdocs). Move your css file there, and try to access it by visiting http://localhost:8888/assets/core_css.css.
Also, in case you are using Linux box, make sure the folders you want to access have read permissions.

Related

Redirect to subdirectory path for the page links having absolute path

For an instance, primary URL is http://example.com running on apache server.
I want to put my project in subdirectories as root>projects>thekingfisher resulting into
http://example.com/projects/thekingfisher/
With htaccess, I could access the project
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /projects/thekingfisher/
</Ifmodule>
But, some of the files have absolute paths, which are not accessible as in below file
/root/projects/thekingfisher/index.php
<html>
<head>
<link href="/css/styles.css" rel="stylesheet" type="text/css">
</head>
<body>
<h1>Test Page</h1>
About
</body>
</html>
As I run my project http://example.com/projects/thekingfisher/
Expected Behaviour:
I want to load the file "css/styles.css" from root/projects/thekingfisher/
Also, "About" link should navigate to "http://example.com/projects/thekingfisher/about.php"
Actual Scenario:
style.css file is being searched at root/css/ to be loaded.
"About" link navigates to "http://example.com/about.php"
Actually, project runs smooth on root. But not properly loading in the subdirectory.
There are many hyperlinks in such pattern, and gets 404 error when tried.
Since this is active project, I don't want to make edits in the source files for modifying the path like prepending baseurl, etc.
You just need a rule in site root to rewrite all requests to /projects/thekingfisher/:
RewriteEngine On
# if file exists in /projects/thekingfisher/ then rewrite
RewriteCond %{DOCUMENT_ROOT}/projects/thekingfisher/$0 -f
RewriteBase .* projects/thekingfisher/$0 [L]

css/js is not working after add htacess

i have .htaccess file and this is a script within it:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^register/$ index.php?c=Page&a=register [L]
</IfModule>
in my layout file, i load css and js with absolute path like this:
<link href="/public/assets/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="/public/assets/bower_components/bootstrap-extension/css/bootstrap-extension.css" rel="stylesheet">
and i access localhost/project/register/ but
the css/js file is not loaded bacause of error:
GET http://localhost/public/assets/bootstrap/dist/css/bootstrap.min.css net::ERR_ABORTED
why it directly access to /public after localhost? it should be localhost/project/public not localhost/public
if i remove first / at css link it access to localhost/project/register/public
Your css link is not correct. It is starting with /public which means Apache will attempt to find it in the public folder under site root.
You should update your path to relative one:
<link href="public/assets/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="public/assets/bower_components/bootstrap-extension/css/bootstrap-extension.css" rel="stylesheet">
And then add this just below <head> section of your page's HTML:
<base href="/project/" />
so that every relative URL is resolved from that base URL and not from the current page's URL.
Alternative to <base ../> tag in HTML, you can use this redirect rule in your .htaccess as very first rule:
RewriteRule ^public/.+\.(?:css|js)$ /project%{REQUEST_URI} [L,NC,NE,R=301]
However do note that a redirect rule (even with R=301) will still make a new client to send 2 requests to your web server. First with /public/... URI and 2nd a /project/public/... URI after redirect.
It seems that you have two separate questions.
For your question, there is nothing in that .htaccess rule you've shown which would change the way that an existing .css or .js file would be displayed. And so, very likely either your links weren't working before, or you had some other content in the file before to rewrite the URLs differently that you have removed. That file is simply stating that if a URL is given and there is no file or directory which matches it, then rewrite the URL for a certain URL. That does seem to be a mistake, since the URL either exists or does not.
Your second question is about the URLs and why you don't have /project in them. I'm not certain why you believe they should have /project in them, but I'm assuming that you had assumed that because your original HTML is in that directory. You are starting your link with a /, as in /public/assets/bootstrap/dist/css/bootstrap.min.css". That / means that the path is absolute, rather than relative, meaning that the path is started directly after the hostname. If you remove the initial /, then the path would be relative to the URL that it is called from.
If you want to load these files relative to the domain root and the public directory is not in the domain root, you'll need to provide the full domain relative path:
<link href="/project/public/assets/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="/project/public/assets/bower_components/bootstrap-extension/css/bootstrap-extension.css" rel="stylesheet">

Codeigniter loads html code when trying to load style.css or a img file

This is my first time using a php framework and I haven't found an effective solution.
I've made my page load style tag correctly using base_url() in an asset_helper file, and it seems to work fine.
generated code:
<link rel="stylesheet" type="text/css" href="http://localhost/p_maricarmen/assets/css/style.css" media="screen" />
but it doesn't load the css file.
I have the same problem with others files like loading images with img tag.
I also have a .htaccess file in root directory with code:
RewriteEngine on
RewriteCond $1 !^(index.php|public|robots.txt)
RewriteRule ^(.*)$ /p_maricarmen/index.php/$1 [L]
for the friendly url.
For more information, if put the style url in my navigator, it generate an html page with 404 error, page not founded.
Modify your .htaccess file with the below code.
RewriteEngine on
RewriteCond $1 !^(index.php|public|assets|robots.txt)
RewriteRule ^(.*)$ /p_maricarmen/index.php/$1 [L]
The reason why your css file is not loading is that you've to specify the Rewrite Condition for the folder where your css and js files have been placed.
You can do the same thing for your images folder.
Simply specify the images parent folder name as i've specified for the assets, and it should work as expected.
Hope this helps you...

external js & css not loading in codeigniter

To load external Javascript file in codeignitor, I'm using
<script src="<?php echo base_url("application/js/contentslider.js"); ?>" type="text/javascript"></script>
I can see in the page's source as
<script src="http://localhost/cinifb_ci/application/js/contentslider.js" type="text/javascript"></script>
which means to my knowledge that the file is loading perfectly similarly for my CSS file, I've used
<link rel="stylesheet" href="<?php echo base_url(); ?>application/css/default.css" type="text/css" />
and in the page's source code its showing as
<link rel="stylesheet" href="http://localhost/cinifb_ci/application/css/default.css" type="text/css" />
which means even its loading perfectly. (on clicking the external js/css link in the source its showing 403 Forbidden). But neither of their effects are applied on the page. No error its showing.
I've loaded URL helper in controller as $this->load->helper('url');
and placed $autoload['helper'] = array('url'); in autoload.php
can you please explain me where I went wrong.
I learn that to load external JS, we need to install jquery-codeigniter library. I've downloaded them and included the respective files into respective similar folders of my application folder.
Now when I try to include $this->load->library('jquery'); in my controller, its showing
An Error Was Encountered
Unable to load the requested class: jquery
Hence I request you to please help me to understand why 403 Forbidden is showing when I can see its pointing to the exact location of the JS/CSS file and secondly if the installation of the jquery-codeigniter library is wrong please guide me.
Note: http://ellislab.com/codeigniter/user-guide/libraries/javascript.html didn't help me
Edit:
jquery-codeigniter library i've placed in system/libraries/javascript/
In systems .htaccess file, I've written as
RewriteEngine on
RewriteRule ^$ /index.php [L]
RewriteCond $1 !^(index\.php|images|css|js|video_files|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ /cinifb_ci/index.php/$1 [L]
In the view I'm writing as
$this->load->helper('url');
<script src="<?php echo base_url("application/js/jquery-1.8.2.js"); ?>" type="text/javascript"></script>
In controller as
$this->load->helper('url');
$this->load->library('javascript');
$this->load->library('javascript/jquery');
$this->load->library('Jquery_ext');
again if I look at source its pointing to correct source but still if i click external js file link, its showing 403 Forbidden error
Sorry for lengthy post :(
Suggestions please ..!
A 403 hidden error means that the Web Server is rejecting the request to that particular resource.
I am going to assume you are using the provided .htaccess file to get pretty urls.
In the line that
RewriteCond $1 !^(index\.php|images|robots\.txt)
Make it read
RewriteCond $1 !^(index\.php|images|robots\.txt|application/js|application/css)
Actually, the above could be wrong, regardless, the idea is there that you need to add your css and js folders to the RewriteCond exception list
The best way is to create folder assets, and then you need to put css, images, js etc.
In this folder save the js/contentslider.js and finally in your header type:
<script type="text/javascript" src="<?php echo base_url();?>assets/css/js/contentslider.js"></script>
Don't forget, you need to set base_url() in config.php.
Thanks
you should include js folder into your .htaccess
if your .htaccess file is inside your application folder then copy it and paste it outside application folder mean root then open it and past code that i have mention and u just need to change name sample in line 3 with your website name or your main folder
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
UPDATE

My php controller can't include css files

The following issue is driving me crazy, perhaps i am thinking to difficult.
Here is the thing, i developed a small MVC framework that works fine. Atleast without url rewriting.
The problem is that as soon as i use url rewrite, things like the css or images which are included in the templates are directed to the wrong directory.
If i type for example: http://www.domain.com/home then everything is fine and the css file get loaded from the http://www.domain.com/css/ directory.
But when i type: http://www.domain.com/home/ the css file won't be loaded, since its looking for the css files in http://www.domain.com/home/css/ Which is obliviously the wrong directory. It seems it sees home/ as a directory where it looks for the the included files.
If i don't use any url rewriting at all, just by typing: http://www.domain.com/index.php?slugs=home/ then there are no problems at all. So i don't think the problem is caused by my script so thats why i think the problems should be looked for in the .htaccess file.
Here is my htacces file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|css|robots\.txt)
RewriteRule ^(.*)$ /astrostrategy/index.php?slugs=$1 [L]
Is there a way, that home/test/whatever(The segments) are not seen as a directory?
Hope my post made sense, i often think to complicated :P
Already thanx for any help! :)
Gr,
Hermes.
If Death's answer doesn't work you can always set the base tag to the url to use in relative paths
it seems you're using relative to currect directory path for css. when you use http://www.domain.com/home/ browser takes home as directory and css file will be located in http://www.domain.com/home/css/. my suggestion is use relative to root path. Now you're using something like :
<link rel="StyleSheet" type="text/css" href="css/style.main.css" />
it's better to use absolute path or relative to root path.
<link rel="StyleSheet" type="text/css" href="/css/style.main.css" />

Categories