1

I am working with codeigniter but i dont know how to put link to another page. My controller filename is aboutus.php. I gave a link like

<a href="<?php echo base_url('aboutus'); ?>">AboutUs</a>

My base url is

$config['base_url'] = "http://localhost/project/";
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';

But above url not working. I write a url directly in browser and hit like http://localhost/project/index.php/aboutus then its working fine. how to give a url? i am confused.

udhaya
  • 79
  • 1
  • 12

6 Answers6

1

base_url() will echo:

http://localhost/project

where as site_url() will echo:

http://localhost/project/index.php

You want to get to http://localhost/project/index.php/aboutus but with base_url() you're only getting to http://localhost/project/aboutus which is giving you the error.

You can do two things,

this:

<a href="<?php echo base_url('index.php/aboutus'); ?>">AboutUs</a>

which means adding the index.php before aboutus

or this:

<a href="<?php echo site_url('aboutus'); ?>">AboutUs</a>

which means changing base_url() to site_url().

Make sure that you are loading the helper in the controller:

$this->load->helper(url);

Or in application/config/autoload.php go to the line which says:

$autoload['helper'] = array();

and do this:

$autoload['helper'] = array('url');

and it will be included in every controller you now have.

If you have short tags enabled you can write your a tag like this:

<a href="<?=site_url('aboutus');?>">About Us</a>

or if you have the url helper you can write it like this:

echo anchor('aboutus', 'About Us', 'title="About Us"');
Albzi
  • 15,431
  • 6
  • 46
  • 63
0

Try site_url() instead of base_url() so index.php will not be skipped

<a href="<?php echo site_url('aboutus'); ?>">AboutUs</a>

https://ellislab.com/codeigniter/user-guide/helpers/url_helper.html

Also, make sure that url helper is loaded.

$this->load->helper('url'); //can also be done in config/autoload.php
AdrienXL
  • 3,008
  • 3
  • 23
  • 39
0

Before using CI functions base_url() and site_url() you need to load URL helper either in autoload.php or in controller itself.

Tpojka
  • 6,996
  • 2
  • 29
  • 39
0

If you have short tags enabled in PHP you can also write it link that:

<a href="<?= site_url('controller_name/function_name') ?>">Link Text</a>
Johnny2k
  • 163
  • 1
  • 7
0

Use this .htaccess on your base folder

RewriteEngine on
RewriteCond $1 !^(index.php|resources|robots.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

If YOu still not get the result Please enable mod_rewrite in your apache server

Arun Yokesh
  • 1,346
  • 17
  • 19
0

your Project URL is look like you didn't enabled php short tags. Thats not a problem. just try this code:

<a href="<?= site_url('index.php/project/aboutus') ?>">About Us</a>