Removing index.php From URLs for CodeIgniter, WordPress, and Other PHP Frameworks

Removing index.php From URLs for CodeIgniter, WordPress, and Other PHP Frameworks

When working with a PHP framework such as CodeIgniter, and I’ve found this same configuration works for WordPress as well, you can eliminate the need to reference index.php as part of the URL, by utilizing the following .htaccess file entry.

** Please note that the syntax for the .htaccess file may be slightly different depending upon your web host. I personally prefer using WestHost and my first examples use such a configuration; however, I have included an example that I found works on GoDaddy hosting as well below the first 3 examples. **

This configuration does require that you have mod_rewrite installed and enabled on your HTTP server. If you have not done so, the configurations will not do anything. You will need to put these directives into a .htaccess file located at the root of your web site, the same location your index.php file resides.

1. Simple Configuration: This configuration is a basic example for removing the index.php file from your web site. It simply takes a request, ensures the file being requested doesn’t exist, and rewrites the URL server side to include the index.php file. The external URL will remain without the index.php allowing your URLs to remain clean. If the actual file being requested does exist, it will not rewrite your URL, and the URL will remain as is allowing the file to be retrieved.

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

2. Moderate Configuration: This configuration builds upon the previous configuration. It takes any request routed to your non-www domain and redirects them through a forced 301 to the www based domain. This keeps all incoming traffic hitting the exact domain you specify. This could also be reversed if you don’t want all of your web addresses to keep the www. prefix. If you utilize subdomains on your web site you may run into some issues with this configuration.

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^myhost\.com$ [NC]
    RewriteRule ^(.*)$ http://www.myhost.com/$1 [R=301,L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

3. Advanced Configuration: This configuration also builds upon the previous configuration. It will remove the index.php from the URL if it exists and force a 301 redirect to a non-index.php URL. I’ve found this to be especially useful if for some reason your pages get indexed with an index.php reference in the URL, such as if Google found your web site before you configured it the way you wanted.

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^myhost\.com$ [NC]
    RewriteRule ^(.*)$ http://www.myhost.com/$1 [R=301,L]
    RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.php(/[^\ ]*)?\ HTTP/
    RewriteRule ^index\.php(/(.*))?$ http://www.myhost.com/$2 [R=301,L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

So, as a recap, the three configurations above will allow you to remove the index.php page from all of your URL requests; and depending upon the configuration you choose, will also allow you to specify the specific domain you want to use or remove the index.php page from URLs through a redirect.

I generally utilize the Advanced Configuration on all of the projects I take on, personal or freelance, and find this accommodates 99.9% of all needs.

4. GoDaddy Basic Configuration: GoDaddy has a slightly different default HTTP server configuration and I had to adjust the directive to accomodate GoDaddy’s servers. You can also apply many of the other configuration directives here; however, I have provided a basic configuration example. The major difference is appending the ‘?’ to the index.php rewrite rule.

<IfModule mod_rewrite.c>
    Options +FollowSymLinks
    RewriteEngine on
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    # FOR GODADDY
    RewriteRule ^(.*)$ index\.php?/$1 [QSA,L]
</IfModule>

Author: daharveyjr

I’m a solution architect responsible for the design, development, implementation, testing, and maintenance of e-commerce operations and applications using the Hybris and WebSphere Commerce product suites and other web technologies such as Java, J2EE/JEE, Spring, PHP, WordPress and more. Twitter | Facebook | LinkedIn

Leave a Reply

Your email address will not be published.