Reputation: 21
this is my code below.
<li><a href="about/why">Why share?</a></li><li>
But after I apply this i manage to redirect to the page but when i try to press another page the url will be keep adding like this and cause page not found.
The page look like this But when i try to link to home page, the url won't change back to /home only Any solution? or another way to link the page? Need help on this! Thanks!
Upvotes: 1
Views: 164
Reputation: 12132
You need to set your base_url()
in config.php
, and call the url_helper
so that you can then use it.
Step by Step Instructions:
in application/config/config.php
, set your base_url
, I prefer to use something like this:
$config['base_url'] = 'http://' . $_SERVER['SERVER_NAME'] . '/';
in application/config/autoload.php
, add url helper:
$autoload['helper'] = array('url');
Use it in your views like this:
<a href="<?= base_url('about/why') ?>" > link </a>
Read this: https://www.codeigniter.com/user_guide/helpers/url_helper.html
Upvotes: 2
Reputation: 2387
Your href should have a slash in front of it so that it goes to "root".
<li><a href="/about/why">Why share?</a></li><li>
If not, the browser will think it is relative to the current route. Or use Codeigniter's built-in site_url() function
<li><a href="<?=site_url("about/why")?>">Why share?</a></li>
Read about relative/absolute here: http://www.coffeecup.com/help/articles/absolute-vs-relative-pathslinks/
Upvotes: 2