Expression Engine - Removing index.php

Wednesday, July 16th, 2008

Expression Engine Removing index.php

Expression Engine ships with no URL rewriting, and that means that every page on your expression engine website will be prefixed with index.php.  To remove the .index.php from each page you’ll need to do some URL rewriting.  Expression Engine have some documents detailing how to remove index.php (opens in a new window) 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 expression engine install called .htaccess.  This will tell Apache how to rewrite the url to remove the offending prefix.  Here’s how to do that:

  1. 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 (opens in a new window) and / or file extentions.
  2. There are two slightly different options now, depending on where you have installed your expression engine site.
    • Installed on the root

      # BEGIN Expression Engine 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 Expression Engine Rewrite
       

    • Installed in a subdirectory

      # BEGIN Expression Engine 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 Expression Engine Rewrite

  3. 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 Expression Engine 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.


8 Comments

Comments RSS | Make a Comment

  1. 03-11-2008

    Perfect! Thanks.


  2. 22-10-2008

    @leonism after you have added the .htaccess file you need to go to your (EE) CP

    Admin > System Preferences > General Configuration

    and remove the line were it say Index.php and click update

    that removed it for me if you havent done that already


  3. 11-09-2008

    @leonism Do you get any errors? If you load a page that should run the rule does your expression engine site load at all?

    If you like I can take a look for you, get in touch and I’ll see if I can help.


  4. 11-09-2008

    @Leevi That’s a great point about the individual files, a nice way around that would be to add some extensions to the rule to be ignored. I’ve added a bit to the rule, that should stop that.


  5. 09-09-2008

    Yes, I met all the technical server requirement just fine. Apache 2.xx, mod_rewrite, .htaccess and everything working out just fine with other cms such as textpattern and wordpress for the permalink structures.
    But when it comes to EE, I tried my methods, they simply don’t work. Oh btw, I’m on site5.com as my hosting provider.
    Many thanks linssen.


  6. 08-09-2008

    Hey Wil,

    There are a couple of major issues using the “File and Directory Check” method of URL rewriting. One of the biggest is no 404 errors for missing images and other files.

    The other issue is a performance issue. If you are missing an image your site will render an entire EE page rather than a simple 404 error. Think of a page with 4 missing images; you would infact be rendering 5 EE pages which will slow down your site a whole bunch.

    I always used the “Include Method” but it also had a couple of issue, namely manually generating the template groups and that pages were not included in the example.

    I have solved this issue with LG .htaccess Generator which automatically generates your .htaccess file for you when an entry or template group is created or modified.

    Check it out here: http://leevigraham.com/cms-customisation/expressionengine/lg-htaccess-generator/

    Cheers Leevi


  7. 04-08-2008

    The rewrite should work regardless of your expression engine version, it’s just looking for ‘index.php/directory’ in the url and then just removing the index.php.

    First off, check that your server is Apache, you could do this by checking with your host, or creating a new php file and adding phpinfo(); to it then uploading and running it: somwhere in there it will have your web server detais.

    Then make sure you’ve got mod rewrite enabled, and there’s a guide to do that here at Wallpaperama.

    Last thing to check is that your .htaccess definitely has no other extensions that are being hidden by your OS and that it’s just called .htaccess and that it’s on the root of your web directory.


  8. 04-08-2008

    None of the above method works for me, I’m on EE 1.6.2. Any ideas why? Many thanks.

Comment On This Post