Saturday, June 14, 2014

[CodeIgniter] Remove the index.php in the URL and using sub folders for controller.

1. Edit .htacess file in www root folder.
This is what I have...

<IfModule mod_rewrite.c>
  Options +FollowSymLinks
  RewriteEngine On
  RewriteBase /
  # If your default controller is something other than 'welcome' you should probably change this.
  RewriteRule ^(welcome(/index)?|index(\.php)?)/?$ / [L,R=301]
  RewriteRule ^(.*)/index/?$ $1 [L,R=301]
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*)$ /index.php/$1 [L]
  SetEnvIfNoCase X-Forwarded-For .+ proxy=yes
  SetEnvIfNoCase X-moz prefetch no_access=yes
  
  # Block pre-fetch requests with X-moz headers.
  RewriteCond %{ENV:no_access} yes
  RewriteRule .* - [F,L]
  # Fix for infinite redirect loops.
  RewriteCond %{ENV:REDIRECT_STATUS} 200
  RewriteRule .* - [L]
</IfModule>

2. Make mod_rewrite enabled in apache configuration file(httpd.conf)
Just uncoment this line.
LoadModule rewrite_module modules/mod_rewrite.so

Make sure AllowOverride is set to All.
AllowOverride All

3. Extend the core Router class of CodeIgniter(for using controller sub folder)
You can obtain the MY_Router.php file at this link and save it to application/core/ folder.
https://degreesofzero.com/article/controllers-in-sub-sub-folders-in-codeigniter.html

4. Restart WAMP server.

5. Tips - If you want to add port based virtual hosts ....

Listen 8080
NameVirtualHost *:8080

<Directory />
#    Require all denied
    Options Indexes FollowSymLinks Includes ExecCGI
    AllowOverride All
    Order deny,allow
    Allow from all
</Directory>

<VirtualHost *:8080>
    ServerName localhost
    DocumentRoot "D:/admin"
</VirtualHost>