udhaya
udhaya

Reputation: 79

Codeigniter url set to another page

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.

Upvotes: 1

Views: 1731

Answers (6)

Albzi
Albzi

Reputation: 15609

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"');

Upvotes: 1

Bipin Kareparambil
Bipin Kareparambil

Reputation: 473

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>

Upvotes: 0

Arun Yokesh
Arun Yokesh

Reputation: 1354

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

Upvotes: 0

Johnny2k
Johnny2k

Reputation: 163

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>

Upvotes: 0

Tpojka
Tpojka

Reputation: 7111

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

Upvotes: 0

AdrienXL
AdrienXL

Reputation: 3008

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

Upvotes: 0

Related Questions