14

For technical reasons, I don't want to use .htaccess to remove index.php from URL in CodeIgniter. I have seen the route section. I also tried with a route like this:

$route['(:any)'] = 'index.php/$1';

But it is not working.

Is it possible to remove index.php using the router in CodeIgniter?

MaxiWheat
  • 6,133
  • 6
  • 47
  • 76
itskawsar
  • 1,232
  • 1
  • 19
  • 30
  • 4
    yes you can using `routes`,it will still work, **BUT** `index.php` will still appear on the url, `htaccess` gives you the pretty url you want and removes the `index.php` on every request depending on what is defined on you're `htaccess` rules – tomexsans Jun 24 '13 at 02:57
  • What exactly is the technical limitation to which you're referring? – djthoms Jun 24 '13 at 03:03
  • if i change the server or path then i need to change `RewriteBase` property in .htaccess. sometimes i dont have authority to change that :( @djthoms – itskawsar Jun 24 '13 at 03:06
  • thanks @tomexsans. Then i can not remove `index.php` from **URL** by routes? And my routes is not working. for example, i have a controller `home` and i will browse it like http://sitename/home. but it showing not found message. :( – itskawsar Jun 24 '13 at 03:11
  • 1
    you can't remove it from url without .htaccess, it is the entry point, actually .htaccess not remove it, just hide it from the url – ikhsan Jun 24 '13 at 08:33
  • removing the index.php from url can be done ONLY with htaccess – machineaddict Jun 25 '13 at 07:19
  • 1
    check my answer, you may not use .htaccess, then you will need a virtual host, or to edit your DNS to point to index.php ! its a server thing not php/framework thing – Zalaboza Mar 12 '14 at 18:31

2 Answers2

9
Is it possible to remove index.php without using .htaccess?

No. it is something beyond php or codeigniter, its a server thing.

Why ?

What happens when user try to go to localhost/codeigniter/welcome/index ?

MVC basically hide all your php files inside its folders. and present only one way for any user to interact with your code through index.php

what happen is

  1. HTTP request to your website directed to index.php
  2. index.php load codeigniter bootstrap and call codeigniter router
  3. router look up the url typed (welcome/index) then go fetch the file welcome from inside controller folder and call index function.

this is plain simple what all MVC frameworks do, hide your code and use a single front file to handle all requests and redirect them using a router class.

so if you want to hide index.php then you need to find a way to tell your server to simply redirect any call to index.php.

index.php is the access door for your website, and your server need to know that all requests must be sent to it. some way or another, and thats why index.php/ must exisit in ur url or use some server configuration to redirect calls to it. but it is always loaded

Community
  • 1
  • 1
Zalaboza
  • 8,899
  • 16
  • 77
  • 142
  • Answer is great, but not cleared. What actually do the CodeIgniter router? I have some apps with lots of custom routing using CodeIgniter router. Can you mention about why i can not remove `index.php` but others string from URL. – itskawsar Mar 13 '14 at 04:00
  • 2
    route is a class.. all it do is get PHP $_SERVER['url'] and explode it to find the correct controller. i think you are missing one thing.. every page you open in your codeigniter project is opened inside `index.php` .. `index.php` is the only public file. other files are Included inside `index.php`, so all your controllers etc are included `include('controller/welcome`)` inside index.php , and the class that handle this include.. is route class – Zalaboza Mar 13 '14 at 07:45
  • 1
    Now i got your point, the router class is also inside index.php. After running `index.php`, the router class works. Thank you. – itskawsar Mar 13 '14 at 13:14
1

I have done the following to work with routes that do not require to have index.php in your url.

  1. I have renamed index.php in index_ci.php for security reasons.
  2. In .htaccess file I have the following:

    DirectoryIndex index_ci.php index.html
    RewriteCond %{REQUEST_FILENAME} !-f  
    RewriteCond %{REQUEST_FILENAME} !-d  
    RewriteCond $1 !^(index_ci\.php) # do not allow direct access  
    RewriteRule ^(.*)$ index_ci.php/$1 [L,QSA]
    
  3. My routes.php now accepts urls without index.php prefix. Example:

    $route['welcome'] = "welcome";
    
shivtej
  • 633
  • 9
  • 18
machineaddict
  • 3,216
  • 8
  • 37
  • 61
  • @lighta: reading his question the second I saw what you said :D – machineaddict Jun 25 '13 at 07:18
  • @machineaddict: thanks. but i can do it with .htaccess, i just need to know the way of without using .htaccess and only with CodeIgniter router :) – itskawsar Jun 25 '13 at 07:32
  • There is just not possible. It doesn't matter if its CodeIgniter, other framework or pure php scripts. Url rewrite happens at apache level, you cannot do it in php. – machineaddict Jun 25 '13 at 09:58