Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 632
MacMac Avatar asked Jul 24 '11 20:07

MacMac


People also ask

What is .htaccess file in CodeIgniter?

htaccess file in CodeIgniter. htaccess is the shortened used for Hypertext Access, which is a powerful configuration file that controls the directory “. htaccess”. It is used by Apache based web servers to control various server features.

Where is .htaccess file located in CodeIgniter?

You should place your . htaccess file at your root directory not Inside the application folder. Show activity on this post. Go to application/config/config.

Why is index php in my WordPress URL?

php appears in the URL might be because the structure of permalinks is not set properly in WordPress Settings. So to verify if the structure of permalinks is set properly, let's check the permalink tab in WordPress Dashboard.


1 Answers

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.

like image 52
ace Avatar answered Oct 01 '22 13:10

ace