qazzu qazzu - 1 year ago 145
PHP Question

Set up the base url in codeigniter

I have the directory structure like this in code igniter:


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 the
when accessing the images or libraries in code igniter. So I tried to change the

$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 Source

In Config.php

$config['base_url'] = 'http://localhost/Appsite/website/';
$config['index_page'] = '';

# If online site
# $config['base_url'] = '';

In .htaccess (outside application folder) - To remove index.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 use base_url load URL helper from autoload.php