Reputation: 34598
I'm trying to learn CI and have just worked through the starter tutorial. I'm finding that some of the link HREFs don't work if my URL ends in a trailing slash - for example, these links:
<?php foreach ($news as $news_item): ?>
<h2><?php echo $news_item['title'] ?></h2>
<div id="main">
<?php echo $news_item['text'] ?>
</div>
<p><a href="news/<?php echo $news_item['slug'] ?>">View article</a></p>
<?php endforeach ?>
The links to stories work fine when I'm on
/index.php/news
but not if I'm on
/index.php/news/
As I say, this is right from the tutorial, so I'm not sure what's up.
I can make the HREFs absolute....
<p><a href="<?php echo config_item('base_url').config_item('index_page').'/news/'.$news_item['slug']; ?>">View article</a></p>
...but figure I shouldn't have to.
Any thoughts?
Upvotes: 2
Views: 3258
Reputation: 61
maybe it is too late, but i have suffered from this problem, despite doing all the suggestion, the problem still! now it works finally, just put
$config['base_url'] = '/myrootfolder/';
instead of
$config['base_url'] = 'http://localhost/myrootfolder/';
Upvotes: 1
Reputation: 2113
Open your config.php then remove the index.php .
Then after that use
echo base_url('news').'/'.$news_item['slug']
Then configure your route like
$route['news/(:any)'] = 'news/index/$1';
Upvotes: 0
Reputation: 4565
Relative URLs won't work how you're expecting them to if you have urls in the following formats:
The reason is because relative urls use the full URI path as a reference (hence "relative"). So, if you reference stuff/things
(note preceding slash) while on the URI path of /stuff/things
(note no end slash), you will get /stuff/stuff/things
because it assumes you want something in the stuff
folder.
What I would do is use CodeIgniter's site_url()
function because it will generate absolute urls (including domain) for you.
site_url('stuff/things')
will become http://domain.com/stuff/things
For this case specifically, use echo site_url('news/'.$news_item['slug']);
within your anchor's href. You can also do echo anchor('news/'.$news_item['slug'], 'Link text to this article');
to echo a link to the article without writing the html.
One caveat-- site_url()
will include index.php
in the URI (if it has not been removed from your config file), so if you want to reference images, javascript, css, etc, then use base_url()
which will only reference your base_path
config variable.
Edit: forgot to mention, you will need to use the URL Helper for these functions to work.
Upvotes: 2
Reputation: 2895
There might not be enough information here to diagnose the problem. "Doesn't work" -- What is the error? What does the full URL look like?
It might be related to the your Apache and server configuration. Try editing the config and experimenting with "PATH_INFO" vs "REQUEST_URI".
| 'AUTO' Default - auto detects
| 'PATH_INFO' Uses the PATH_INFO
| 'QUERY_STRING' Uses the QUERY_STRING
| 'REQUEST_URI' Uses the REQUEST_URI
| 'ORIG_PATH_INFO' Uses the ORIG_PATH_INFO
|
*/
$config['uri_protocol'] = 'AUTO';
Lastly, (this is only slightly related) but the best way to build URLs in Codeigniter is the site_url() function (which creates a full URL):
echo site_url("news/local/123");
Upvotes: 2
Reputation: 129
make it like this:
<a href="/news/<?php echo $news_item['slug'] ?>">View article</a>
just adding the slash at the begining
UPDATED:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond $1 !^(index\.php|uploads|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
add this to your .htaccess file in the root of your application AND in the config file of codeigniter:
$config['index_page'] = ''; //make it like this
now you don't have to use the /index/news, just use the /news from the root of your site
so then you can use it like this:
<a href="/news/<?php echo $news_item['slug'] ?>">View article</a>
it shut work
Upvotes: 2