Search code examples
apachemacoscodeigniterosx-snow-leopard

Remove "index.php" from URL - Codeigniter


I've looked in the documentation of Codeigniter of removing the index.php from the URL when accessing different views, the code shows how to remove it with apache:

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

However, when I go to http://localhost/code/home, I get this:

The requested URL /code/home was not found on this server.

But accessing http://localhost/code/index.php/home works just fine, how come it isn't working for me?

I'm on a Mac OS X Snow Leopard using the Sites directory: /Users/~myusername~/Sites/code, and I'm not using any software, i.e. MAMP, XAMPP.

EDIT

For the sudo /usr/sbin/apachectl -k restart -S command, I get this:

VirtualHost configuration:
wildcard NameVirtualHosts and _default_ servers:
*:80                   is a NameVirtualHost
         default server code.local (/private/etc/apache2/httpd.conf:146)
         port 80 namevhost code.local (/private/etc/apache2/httpd.conf:146)
Syntax OK

Solution

  • You need to make "http://localhost/code" your web root as 'code.local' (in my example). Assuming that your setup on Mac OS X Snow Leopard is the same as mine.

    You should add the following lines to "/etc/apache2/extra/httpd-vhosts.conf"

    <VirtualHost *:80>
        DocumentRoot "/Users/~myusername~/Sites/code"
        ServerName "code.local"
        ErrorLog "/private/var/log/apache2/code-error_log"
        CustomLog "/private/var/log/apache2/code-access_log" common
    </VirtualHost>
    

    Also make sure that it is uncommented in "/etc/apache2/httpd.conf"

    # Virtual hosts
    Include /private/etc/apache2/extra/httpd-vhosts.conf
    

    Then you add code.local to your "/etc/hosts" which should look like this

    ##
    # Host Database
    #
    # localhost is used to configure the loopback interface
    # when the system is booting.  Do not change this entry.
    ##
    127.0.0.1       localhost
    127.0.0.1       code.local
    

    Then restart your apache "/usr/sbin/apachectl -k restart".

    You might need to sudo if you are not a super user. Hope this helps.

    EDIT: To check if your virtual host works. Run this sudo /usr/sbin/apachectl -k restart -S To see if code.local has been added as one of your virtual hosts.

    Then try accessing code.local/index.php/welcome and code.local/welcome to check if it works.