php - Set up the base url in codeigniter
Get the solution ↓↓↓I have the directory structure like this in code igniter:
Appsite
-website
-application
-images
When I accessing the image in index.php I used:<img src="http://localhost/Appsite/website/images/images.PNG"
And the href is:<li class=""><a href="http://localhos/tAppsite/website/index.php/home/">Home</a></li>
I think it is not a good practice to include thehttp://localhost
when accessing the images or libraries in code igniter. So I tried to change the$config['base_url']
inconfig.php
to$config['base_url'] = "http://".$_SERVER["HTTP_HOST"]."/";
And now I update my image source and other library source I remove the localhost and the name of my directory folder:
<img src="images/images.PNG”>
<li class=""><a href= <?php echo base_url;?> /website/index.php/home/">Home</a></li>
But I get errors. it says object not found. Can some help me?
Answer
Solution:
In Config.php
$config['base_url'] = 'http://localhost/Appsite/website/';
$config['index_page'] = '';
# If online site
# $config['base_url'] = 'http://stackoverflow.com/';
In.htaccess
(outside application folder) - To removeindex.php
in URL
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|image|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
To accessing URL
<a href="<?php echo base_url();?>contollerName/methodName"> click here</a>
To access image
<img src="<?php echo base_url();?>images/images.PNG”>
To access CSS
<link rel="stylesheet" type="text/css" href="<?php echo base_url();?>assets/css/style.css"/>
To usebase_url
load URL helper from autoload.php
Answer
Solution:
In yourconfig.php
set thebase_url()
as,
$config['base_url'] = 'http://localhost/projectname/';
In your view load the image as,
<img src="<?php echo base_url();?>images/images.PNG”>
Answer
Solution:
Just put this it will take the automatically correct path of the project and set base URL
$site_url = ((isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https' : 'http';
$site_url .= '://' . (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '');
$site_url .= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
$config['base_url'] = $site_url;
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: a non-numeric value encountered in
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.