other than home page css file not loading in codeigniter - php

i configure website on my local host using XAMPP but other rhan homepage the css files not loding
my .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
config.php
$config['index_page'] = '';
$config['base_url'] = 'http://hypertradeproject.local/';
on home page css link like.
<link rel="stylesheet" href="assets/css/bootstrap.css">
<link rel="stylesheet" href="assets/css/bootstrap.min.css">
when i open page source they are like
http://hypertradeproject.local/assets/css/bootstrap.css
and on all other pages they are like i.e
<link rel="stylesheet" href="assets/css/bootstrap.css">
<link rel="stylesheet" href="assets/css/bootstrap.min.css">
when i open page source they are like
http://hypertradeproject.local/site/cat_one_subcat/assets/css/bootstrap.min.css
http://hypertradeproject.local/login/assets/css/bootstrap.min.css
they have to be like
http://hypertradeproject.local/assets/css/bootstrap.min.css
http://hypertradeproject.local/assets/css/bootstrap.min.css
im new to codeigniter. any suggestions.
thanks in advance.

First load url helper in application/config/autoload.php using
$autoload['helper'] = array('url');
OR you can load url helper in controller using $this->load->helper('url');
then link your css files like below:
<link rel="stylesheet" href="<?php echo base_url('assets/css/bootstrap.css');?>">
<link rel="stylesheet" href="<?php echo base_url('assets/css/bootstrap.min.css');?>">

$config['root_dir']= "/"; Add this line in your
application/config/config.php file
then inlcude your css file like this
<link rel="stylesheet" href="<?php echo $this->config->item('root_dir')?>assets/css/bootstrap.css">
<link rel="stylesheet" href="<?php echo $this->config->item('root_dir')?>assets/css/bootstrap.min.css">

You can simply add site_url in front of path.
Thanks

Its working heres the two diffrent ways.
Either place base_url (base url whic we defign in application/config/config.php) at the start in href like this.
<link rel="stylesheet" href="<?php echo base_url();?>assets/css/bootstrap.css">
or just add "/" start of href like this.
<link rel="stylesheet" href="/assets/css/bootstrap.css">

Related

Subfolder don't load the style.css

I have a .htaccess file that redirects all the URL request to my index.php
AcceptPathInfo On
RewriteEngine on
RewriteBase /Projects/tester/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?request=$1 [L,QSA]
Inside my index.php I have a code that include the URL file:
<?php
if(isset($_GET['request'])){
include('pages/'.$_GET['request'].'.php');
}else{
echo 'index';
}
?>
If my URL is localhost/Projects/tester/green my index will include a file called green.php that exists inside the folder pages.
inside pages/green.php I have this code:
<link rel="stylesheet" type="text/css" href="style.css">
<div id="greenBox">
This box is green!
</div>
When I run that URL I can see a large green div with an text inside.
Great, now lets create a folder called subs inside the folder pages and create a file called yellow.php
<link rel="stylesheet" type="text/css" href="style.css">
<div id="yellowBox">
This box is yellow!
</div>
When I run this URL localhost/Projects/tester/sub/yellow I can see the text but the style.css don't load (I can't see the yellow div).
If all the content is being included in the index.php and style.css file is in the same folder as the index.php, why green.php can load the style.css but the yellow.php not?
I can see the yellow div only if I add ../style.css inside the yellow.php, but if it is to work on this way, I believe we should use ../style.css on green.php while in yellow.php use ../../style.css, no?
Is there a way to solve this? is problem in .htaccess?
Of course the external browser doesn't understand where your files are located on the server, so if you are in the following situation (from the browser's perspective):
http://localhost/Projects/tester/green
-> hey browser, load "style.css"
-> browser loads http://localhost/Projects/tester/style.css
But if you change the "virtual" path:
http://localhost/Projects/tester/sub/yellow
-> hey browser, load "style.css"
-> browser loads http://localhost/Projects/tester/sub/style.css
So you have three solutions here, in order of my personal preference:
1) Use absolute URLs for loading css data, optionally using a PHP var:
<link rel="stylesheet" type="text/css" href="/Projects/tester/style.css">
<link rel="stylesheet" type="text/css" href="<?php echo $base_path; ?>/style.css">
2) Use the <base> html element in head: (warning: it applies to ALL relative urls in the page: links, images, etc.)
<head>
<base href="http://localhost/Projects/tester/" />
</head>
3) If using absolute URLs is not an option, you can calculate how many "virtual subdirectories" you are down and generate a prefix like this:
<?php
// first calculate $virtal_subdirs_number
$relative_urls = str_repeat("../", $virtual_subdirs_number);
?>
<link rel="stylesheet" type="text/css" href="<?php echo $relative_urls; ?>/style.css">
Its because the style.css is in parent directory of yellow.php, So for accessing a file in parent directory you must have to give path like ../style.css which mean look for it in one directory back and /style.css mean in the current directory which contain yellow.php

.htaccess url: rewrite missing css javascript and images file at local host Xampp

I am new on .htacees. I am trying to make user friendly URL for users using mode rewrite. My problem is that when i open "user.php" page using .htaccess re:write it can't load the CSS file or images file and show like this.
Sign up
My account
Hi
My Profile
My account
Logout
Help
© 2014 Company, Inc.
I have a link like:
http://localhost/www.zeeshan.com/user.php?name=zeeshan06
New link is something like and its work 100% right for me.
http://localhost/www.zeeshan.com/user/zeeshan06
my .htaccess file
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /www.zeeshan.com/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^user/([^/]+)$ user.php?name=$1 [NC,L]
My CSS file path css/style.css , css/bootstrap.css, bootstrap.min.css in the www.zeeshan.com folder
<head>
<link href="style.css" rel="stylesheet" type="text/css"/>
<link href="css/bootstrap.css" rel="stylesheet" type="text/css"/>
<link href="css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
</head>
with out .htaccess page work well and show css of div,panel etc. But using .htaccess
re:write its cannot open css file and images file. please help me to solved my problem. Thanks..
Your paths for your CSS files are relative. So when you load up http://localhost/www.zeeshan.com/user/zeeshan06, your browser is looking for CSS files in a folder called http://localhost/www.zeeshan.com/user/. You need to change your paths to:
<head>
<link href="/www.zeeshan.com/style.css" rel="stylesheet" type="text/css"/>
<link href="/www.zeeshan.com/css/bootstrap.css" rel="stylesheet" type="text/css"/>
<link href="/www.zeeshan.com/css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
</head>
If you put a static link on each page inside the <base> tag you'll need to change it all over again after deploy.
use this:
<base href= "<?php echo "http://".$_SERVER['HTTP_HOST']."/your_folder/" . "/>" ; ?>"
so you'll can upload to production and will never need change it again.

exclude css file and image on url rewriting in php

i have problem in loading css file and image file while using url rewriting with this rules :
RewriteEngine on
RewriteRule ^showProject-([^/]+)-([0-9]+)\.html$ showProject.php?id=$2
i get this url :
localhost:8080/coders_ring/project/test/1.html
real url is :
localhost:8080/coders_ring/showProject.php?id=1
also i test in get url css file like this but not solution :
<link href="style/master_style.css" rel="stylesheet" type="text/css" />
<link href="/style/master_style.css" rel="stylesheet" type="text/css" />
any body have a solution to load css and image file with this url rewriting ?
Try to add a base href tag in your showproject.php file
Add this
<base href="http://localhost/coders_ring/" />

How to load css in codeigniter

I am new to codeigniter, and I am using v.2.12. I am getting an error when I try to load the css from the external file.
I create the css folder inside the application folder. And I create the css file in the name of all.css.
In the view file I use the following code to link the css file.
<link rel="stylesheet" type="text/css" href="<? echo base_url();?>css/all.css">
But the css file is not loading. I'm getting 404 error. Here is my configuration settings:
$config['base_url'] = 'http://webscarlets.com/ci/index.php';
$config['index_page'] = 'index.php';
Website Link: http://webscarlets.com/ci/index.php/welcome.
This is how you include CSS files in CodeIgniter:
<?php echo link_tag('css/mystyles.css'); ?>
That snippet will output this HTML:
<link href="http://site.com/css/mystyles.css" rel="stylesheet" type="text/css" />
The function link_tag is in the HTML helper, which must first be loaded.
(Note that you probably shouldn't put your CSS files in /application/css. It's just easier to put them in /css or maybe /assets/css.)
The function base_url() should return the base path (without index.php)
You may fix it by adding a backslash like:
<link rel="stylesheet" type="text/css" href="<? echo base_url();?>/css/all.css">
or remove the index.php from your config:
$config['base_url'] = 'http://webscarlets.com/ci/';
I just find the solution to avoid index.php file and load ours CSS files.
Just copy the code below in a .htaccess file:
Options +FollowSymLinks
Options -Indexes
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|styles|scripts|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
Greeting!
To attach a CSS, JS, Images .etc you just have to do is go to your config folder and write at the end of the constant.php file.
define('URL','ADD YOUR LOCAL/REMOTE PATH');
define('CSS',URL.'public/css/');
define('IMAGES',URL.'public/images/');
define('JS',URL.'public/images/');
After that goto your view and in the link just add
<link rel="stylesheet" type="text/css" href="<?php echo CSS; ?>index.css">
this will solve your problem.
Hope it helps.
Add below line in your controller action /application/controllers/yourController.php
$this->load->helper('url');
Then add below line to your view file's head tag.
<link rel="stylesheet" type="text/css" href="<? echo base_url('assets/css/yourcssfile.css');?>" />
Assuming that you have assets/css/ folder is created in your app directory.
<path to your app folder>/assets/css
before using the base_url() you should have to load the URL helper class.
something like $this->load->helper('url'); in your controller
base_url() return you the path something like
'http://webscarlets.com/'
if you have set it directly in the root or 'http://webscarlets.com/dir/'
and also make sure about the location of your CSS file.
follow the link to know more about URL Helper
another way would be
define a constant in constants.php (in config directory)
define("LAYOUT_URL","http://localhost/yoursite/css/");
"css" folder here i m assuming is inside application folder. NOw you can attach css in page like
<link rel="stylesheet" type="text/css" href="<?php echo LAYOUT_URL;?>all.css">
As Jogesh_p.
you use base_url as follow
put in controller (your controller)
$this->load->helper('url');
in controller .
if you want to use
as follow
put in where you want use base_url.
echo base_url()
NOTE: better you create new folder at root
(Example: theme)
same: application, system, user_guide, theme)
i hope can you do
Edit autoload.php as follows
$autoload['helper'] = array('url');
Then load css , js,image like that
img src="<?php echo base_url(); ?>assets/images/master.jpg"</img>
//config.php
$config['base_url'] = 'http://webscarlets.com/ci/';
$config['index_page'] = 'index.php';
and try to load css by adding application folder
<link rel="stylesheet" type="text/css" href="<? echo base_url();?>application /css/all.css">
EDIT
Here
base_url() echos 'http://webscarlets.com/ci/' then adding the file with path application /css/all.css
Include $this->load->helper('html'); in a controller function.
And use linktag key word in view file something like this:
<html>
<head>
<title></title>
<?php echo link_tag('resources/style.css');?>
</head>
<body>
<?php
....
?>
</body>
Here the resources is the folder that contain the style.css file.

Problem with removing index.php on Codeigniter didn`t load my css and js

Currently I tried to remove index.php on codeigniter url. I installed my codeigniter on localhost/jqm/withci . Then I write .htaccess to remove index.php and it worked well.
.htaccess file :
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /jqm/withci/index.php/$1 [L]
But Im having some problem, my css file and js wont load.. My conroller is
class Home extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->helpers('url');
}
function index() {
$this->load->view('head_view');
$this->load->view('main_view');
$this->load->view('foot_view');
}
}
my view for head_view is :
<!DOCTYPE HTML>
<html>
<head>
<meta name="viewport" content="width=320; user-scalable=no" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
<title>jQuery Mobile with Codeigniter</title>
<link rel="stylesheet" href="<?php echo base_url();?>jquery.mobile/jquery.mobile-1.0a4.1.css" type="text/css" charset="utf-8" />
<script type="text/javascript" src="<?php echo base_url();?>jquery.mobile/jquery-1.5.2.js"></script>
<script src="<?php echo base_url();?>jquery.mobile/jquery.mobile-1.0a4.1.js"></script>
<!-- CDN Respositories: For production, replace lines above with these uncommented minified versions -->
<!-- <link rel="stylesheet" href="http://code.jquery.com/mobile/1.0a4.1/jquery.mobile-1.0a4.1.min.css" />-->
<!-- <script src="http://code.jquery.com/jquery-1.5.2.min.js"></script>-->
<!-- <script src="http://code.jquery.com/mobile/1.0a4.1/jquery.mobile-1.0a4.1.min.js"></script>-->
</head>
<body>
I tried to use base_url() but it didnt load my css and js.
Thank you..
PS : Im using css and js file for jQuery Mobile. We can load it from CDN repositories, but I want to make it loadable from my own directory in case I want to load custom css or js in future
In your .htaccess file add the folder which contains the JavaScript and CSS to the rewrite condition, something like:
RewriteCond $1 !^(index\.php|images|jquery\.mobile|robots\.txt)
Currently those files aren't loading, instead they're pointing to your index.php file.
Try adding this to your rewrite rules to prevent the rewrite from occuring on all files and directories:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
you can set a new config variable at the config file and but the path for the css/js file on it

Categories