Exegesis
Exegesis

Reputation: 1078

CodeIgniter URL Segment-Based

I want my URL's to look like: www.domain.com/controller/view/args instead of index.php?/controller/view/args. Actually, I NEVER want the url "index.php?..." to validate. Can anybody give me a hint on how to enforce this behavior, because when I do: A) redirect() or B) type in manually 'index.php'"..." the url changes to that ugly pattern.

This is my .htaccess file (I don't know if I need this file, just copy+pasted from the internet):

#http://codeigniter.com/wiki/Godaddy_Installaton_Tips/

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?$1 [L]

This is my config.php:

$config['base_url'] = '';
$config['index_page'] = 'index.php?';
$config['uri_protocol'] = 'AUTO';
$config['enable_query_strings'] = FALSE;

And this is what changes the behaviour:

redirect('...blah', 'either location or refresh');

Upvotes: 3

Views: 749

Answers (3)

solidau
solidau

Reputation: 4081

From the CI Wiki: http://codeigniter.com/wiki/mod_rewrite/

Upvotes: 1

Robbie Scourou
Robbie Scourou

Reputation: 136

In addition to needing to place the .htaccess in the root of the codeigniter install (in the same place as the index.php), you'll also need to ensure that mod_rewrite is working on your Apache install, otherwise the .htaccess file will just be ignored.

Additionally, you need to set the AllowOverride directive in your Apache virtual host (see http://httpd.apache.org/docs/2.0/mod/core.html#allowoverride) - it should be set to something like:

<VirtualHost *:80>
...
AllowOverride All
...
</VirtualHost>

You should also change:

$config['index_page'] = 'index.php?';

To:

$config['index_page'] = '';

Upvotes: 1

Howard Grimberg
Howard Grimberg

Reputation: 2178

You actually need the .htaccess file in the root of the codeigniter directory(wherever index.php is). The .htaccess file is responsible for managing the rewriting of parameters. if you query yoursite.com/?controller/model, it will work the same as if index.php were there. The "?controller/..." will still be there, but index.php will not be there as long as you remember to always link to the non index.php version. The included .htaccess will work in most cases (especially with apache). You may need to modify if codeigniter is not in the root of your site(i.e. public_html/somedir/{codeigniter_goes_here}).

Upvotes: 2

Related Questions