How to remove index.php from your ExpressionEngine URLs

First, two things:

What the heck is a .htaccess file and why do I care?

.htaccess is a file that directs your web server how to handle requests for files.  Sounds exciting, no?  Like many things Linux, this little text file has the power to make your site do all sorts of cool things, as well as to break it completely and have you speed-dialing tech support, pleading for a fix. 

Apache server configuration?

There are many ways to configure an Apache server (typically used to run ExpressionEngine).  Because of this variation, what works on one server can fail miserably on another.  For example, several times in the past, I’ve tried using Leevi Graham’s LG .htaccess Generator, but always failed. The first two times I followed the instructions, I received a dreaded Server Error 500, and I could not access my site nor the control panel.  Fortunately, the nice tech support guy at Engine Hosting removed one offending line, and my site was restored. So…caveat emptor (buyer beware).  The following trick is known to work on these two hosting services, at minimum: Engine Hosting and Rackspace.

Step 1

Before you do anything else, backup your database and files!  We take no responsibility for any type of hosedness your site incurs by following these instructions.

Step 2

Download and install the LG .htaccess Generator extension.  You’ll need to update one setting in the extension, labeled “The server directory where your .htaccess will be generated”. This will typically be something like /www/your_account/public_html/, and refers to the root directory where you’ve installed ExpressionEngine.  If you don’t know what it is, go to Utilities—> PHP Info, and look for the value of _SERVER[“PHP_DOCUMENT_ROOT”].

Step 3

Create a .htaccess file at your server root.  Note: Windows operating system does not like files that start with a period.  Windows sufferers, try this: create a file on your computer called htaccess.  Upload it to the server root, then rename it to .htaccess. That’s right, it starts with a period.  Also note: your FTP program may not want to show you files that start with a period.  You may have to turn on filtering.  The -a setting should allow you to see the file.

Now CHMOD the .htaccess file to 666.  This is often done by right-clicking on the file.

Step 4

Go in to the LG .htaccess Generator settings. Where you see “.htaccess rules”, you need to remove the following lines:

# Dont list files in index pages
IndexIgnore *

Why?  Because these lines will cause your site to crash on Engine Hosting server (and presumable elsewhere).

Also, add this line:

# Allow calls to css files
RewriteCond %{QUERY_STRING} ^(css=.*)$ [NC]

This will allow you to call CSS files that are ExpressionEngine templates. 

And finally, these lines:

# deal with various ACT urls that may otherwise be disrupted
RewriteCond %{QUERY_STRING} !^(ACT=.*)$ [NC]

This will allow membership-related URLs to function.*

Submit your changes.  This should write to the .htaccess file your created earlier.

Step 5

Go to Admin—> System Preferences—> General Configuration, and set “Name of your site’s index page” to be blank.  Since you’ve removed index.php from all URLs, make sure your weblog preferences reflect this. For example, if you have a “staff” weblog, its URL might be yoursite.com/staff, rather than yoursite.com/index.php/staff

Step 6

Rejoice!

Note: If you want to try something less daring, you can also Replace index.php in ExpressionEngine URLs, a much simpler technique.

*Thanks to Aidann Bowley for this tip.