Musings

ExpressionEngine - Removing index.php

16th July 2008

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 (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 ExpressionEngine 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 expressionengine site.

  1. 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

  1. 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

  1. 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.

Discuss

Get involved in the discussion by using the comment form below.

leonism

4th August 2008

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

wil.linssen

4th August 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.

Leevi Graham

8th September 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

leonism

9th September 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.

wil.linssen

11th September 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.

wil.linssen

11th September 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.

Carl

22nd October 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

Rolando Murillo

3rd November 2008

Perfect! Thanks.

OJ

8th December 2008

ive tried this method, and it works and all. but when i try to use the search, it takes me to an 404 error page?

wil.linssen

19th December 2008

What’s your site OJ? I’ll take a look.

Jason

18th March 2009

Great solution, it worked great.

Thanks

Friso

5th April 2009

Hi there. I tried hundreds pieces of codes for .htaccess. None of theme worked. I contacted my webhost, they said that they support mod_rewrite, so it’s possible. I copied the code and pasted into a .htaccess file, uploaded and reloaded my website. It won’t work. HELP ME!

wil.linssen

5th April 2009

@Friso, you might want to check that your web server is Apache and that Mod rewrite is defintely on. There are some comments further down to help you with that, try them then let me know how you get on.

Michael Biven

10th July 2009

Has anyone had issues with every page returning as a 404 when using a custom 404 template set in global template preferences with the index.php removed?

John Knotts

11th July 2009

awesome, thanks again!

Jon

13th July 2009

Hello. I have tried this method and it work apart from when I try to do a seach (as it does for @OJ) and just goes to the homepage

wil.linssen

13th July 2009

@Jon do you have a template group for your search results?

Jon

13th July 2009

@wil.linssen I have created a ‘people’ section on my site and the search result templates are part of this group

wil.linssen

14th July 2009

I ask because if you don’t have a 404 page set up if ExpressionEngine can’t find the page, it will just display your default template. Perhaps just double check your results and no results pages are present and correct? http://expressionengine.com/docs/modules/search/simple.html#par_no_result_page

designcoup

27th August 2009

Wow. I have tried so many methods to remove the index.php and none worked until now. Your solution is the most direct and easiest to implement that I have found. Thanks!

Jon

7th September 2009

Hello.

If have added a 404 page but it wont work when your rewrite code is included within the same .htaccess file!

The search is working fine when I don’t try to hide the index.php (or eeindex.php as I’m using). If I use your code search does not work, although eeindex.php is hidden in the URL. Do you have any suggestions?

toby powell

16th September 2009

nice one, fixed and lovely and shiney!

Sarah

13th November 2009

You sir are a legend! I have been pulling my lovely locks out all week and bam! you had the answers all along! thank you!

Wil Linssen

13th November 2009

You’re most bloody welcome @Sarah, I hope you’ve got some lovely locks left.

Gavin Cooper

8th December 2009

Thanks for the tutorial. Although, currently the homepage displays for all template groups. so…

domain.com/about
domain.com/story

All show the homepage

J.T.

8th January 2010

Hi Wil - first, thanks for the quick fix. This seems far less complicated than the fixes on the EE forums.

However I’ve run into a snafu - after following your instructions to the letter, I’ve found that I still have the “?” remaining in the addresses. Instead of

http://www.jtroll.com/jtroll/projects_main/

I get

http://www.jtroll.com/?/jtroll/projects_main/

Odd things happen if I try to type in the address manually without the “?” as well. Any ideas? Run into this before, perhaps?

Cheers!

Wil Linssen

8th January 2010

@J.T. You might do well to check the Name of your site’s index page in CP Home ›  Admin ›  System Preferences ›  General Configuration. Just set it to nothing (blank)

Elliot

15th January 2010

Great stuff, thanks for the tip there Wil. Much clearer to follow than the official documentation.

I had a 404 glitch and after much hair-pulling I realised that I had copied .htaccess to a subdomain by mistake! Annoying! Works fantastically now. It’s funny that this is much cleaner than the ‘rename index.php to some extensionless file’ method suggested by EE, and yet is more of a by-note.

Thanks for the effort.

Edwin

1st March 2010

Thanks. Works like a charm, AFTER I realized I should have named the file .htaccess (with the dot) and not htaccess (without dot) smile

Used Engines

28th April 2010

I love this stuff!

-jman
webmaster at
Used Engines

Trent

10th May 2010

Does this interfere with google (search engine) indexing..?

Works great by the way, just concerned there may be a down side to it?

Wil Linssen

10th May 2010

@Trent It’s better for search engine indexing - the urls make more sense.

Trent

10th May 2010

OK.
And what about handling 404 errors?
I am struggling to get 404 working properly with the Structure plugin with invalid URL’s.

When I apply a template in Global Template Preferences for 404’s it treats everything as invalid URL.

Any help?

kemar

27th June 2010

Hey,

It worked for a minute but doesn’t for some reason where the home page works, but the sub pages like about, contact presents page not found. I am using the subdirectory method. I don’t know if it’s that I am using the expression engine 2.0 and this was written for previous version.

On the same note Leevi generator is stuck as the extensions manager is throwing an error of something like extension refresh thing. Please check!

kemar

27th June 2010

Hi,
I did some acessement of the subdirectory and it worked. However, for some of the pages such as news, i still see the index.php extension why?
Here are some details.
Host:a2hosting
Apache version   2.2.13
PHP version   5.2.9
EE 2.0

I am using the agile theme sample given to this version.

Thanks

sujitha

16th July 2010

hey thanks for your opinoin..
it helped me to remove index.php from the url but i have a question mark in that place..
can anyone assist me in this

Leave a comment

Your email is never ever shared, and required fields are marked with *

*

*

Used for Gravatars



Sorry folks, uses the "nofollow" attribute

*


My apologies for this nonsense: please type the word you see below.



Copyright © 2009 Wil Linssen all rights reserved

Wil-Linssen.com is powered by ExpressionEngine: it's good, go and get it.