ExpressionEngine - Removing index.php

ExpressionEngine ships with no URL rewriting, and that means that every page on your ExpressionEngine website will be prefixed with index.php. To remove the .index.php from each page you’ll need to do some URL rewriting. ExpressionEngine have some documents detailing how to remove index.php From URLs on their website, but they don’t cover if you’re hosting your site from a subdirectory (www.mysite.com/subdirectory/expressionengine/)

To remove index.php from your pages, just create a file in the root of your ExpressionEngine install called .htaccess. This will tell Apache how to rewrite the url to remove the offending prefix. Here’s how to do that:

Use your favourite text editor to create a file called .htaccess – chances are at this stage you might have to turn off extensions or put the name in quotes “.htaccess” to ensure it’s not affixed with a .txt extension or the like. Double check it’s right by showing hidden files and / or file extensions in os x.

There are two slightly different options now, depending on where you have installed your ExpressionEngine site.

Installed on the root

# BEGIN ExpressionEngine Rewrite

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteRule ^(.*)$ /index.php?$1 [L]
</IfModule>
# END ExpressionEngine Rewrite

Installed in a subdirectory

# BEGIN ExpressionEngine Rewrite

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /subdirectory/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteRule ^(.*)$ /subdirectory/index.php?$1 [L]
</IfModule>
# END ExpressionEngine Rewrite

Save and upload

Both these methods will check for physical directories on the server, so if you have a directory called images etc. they won’t be rewritten.

I had some problems under some Apache installs with the rule, and recently altered it. You might notice that your rule is almost working but not quite – the page / template urls are loading, but only the home page is coming up? I fixed this by changing the trailing slash on the index.php in the rule to a ? that way there is no interpretation of the url, the variable is handed straight to the php.

That means the rule will read like (with no sub directory)

RewriteRule ^(.*)$ /index.php?$1 [L]

Leevi Graham also pointed out that there might be some problems with images or other single file types, so I added the rule that would omit certain file-types from the rewrite:

RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]

Carl pointed out that you’ll also need to alter your ExpressionEngine configuration so that the default page will not include index.php. To do that, login to your control panel and go to Admin >System Preferences >General Configuration remove the line were it says index.php and click update.