0

I'm working on a project in Codeigniter that doesn't use the standard routing.php file. It calls the functions like this: test.vi/index.php/controller/function.

My goal is to remove the index.php from the url with a htaccess rewrite:

 RewriteEngine on
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule .* index.php/$0 [PT,L] 

And also this config.php change:

$config['index_page'] = '';

My question is do I need to route all the controllers/functions in my route.php or can it be done without using the route.php file?

0

4 Answers 4

3

first make sure you enabled mod_rewrite in httpd.conf:

to do so add/make sure it exsits this line

LoadModule rewrite_module modules/mod_rewrite.so

then create .htaccess in your codeigniter root folder (next to index.php file) not application folder.

.htaccess

DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|img|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]

codeigniter take care of all routings for you; so for

controlers/welcome.php method=hello u can access it by

http://localhost/welcome/hello

its not like laravel you need to specifically add route; codeigniter automatically Fitch controller for you. unless you have it stored in a different location or inside subfolder then you dont need to touch route.php except for setting your default route.

Sign up to request clarification or add additional context in comments.

2 Comments

I tried your .htaccess still getting a 404 if I don't add index.php to the url.
did you enable rewrites in you server ?
0

Here's my redirect in .htaccess for a live codeigniter site. The second part exempts certain directories.

#Redirect non-www to www:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

RewriteCond $1     !^(index\.php|phpinfo\.php|test\.php|example\.php|images|i|css|js|beta|captcha|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]   

Comments

0

THIS works perfect for me... give it a try..

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

2 Comments

using RewriteCond $1 !^(index\.php|img|css|js|robots\.txt|favicon\.ico) allow you to skip redirection for files/folders u specify; also avoid blocking search engines
Tried it same as the .htaccess below still 404. any ideas?
0

try this

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

I guess everyone forget about add test.vi folder name on .htaccess

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.