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.

6 Answers 6

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"');
Sign up to request clarification or add additional context in comments.

2 Comments

how to avoid index.php in url?
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

Comments

0

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

Comments

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>

Comments

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

Comments

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.