Code Igniter - relative URLs stop working when URL ends in slash? Code Igniter - relative URLs stop working when URL ends in slash? codeigniter codeigniter

Code Igniter - relative URLs stop working when URL ends in slash?


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 onRewriteBase /RewriteCond $1 !^(index\.php|uploads|robots\.txt|favicon\.ico)RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule . /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


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


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.