Services Newcastle’s most advanced web company. Learn more ›

Photo of Leevi Graham

Leevi GrahamCode Monkey

Newism Pty Ltd
Newcastle, NSW Australia

LG .htaccess Generator - Removing index.php from your ExpressionEngine site

One constant frustration ExpressionEngine users have is, how do they easily remove index.php from their site.

The first point of call is usually “Remove index.php From Urls” documentation in the ExpressionEngine Wiki which lists three main methods and their caveats. The three methods listed in the wiki are:

  1. Include List Method
  2. Exclude List Method
  3. File and Directory Check Method

Each method has their good and bad points which are listed below:

1.  Include List Method

Good: Only affects ExpressionEngine template groups and pages.

Bad: Template groups and pages must be manually added to the .htaccess file each time a new one is created. This unfortunately is something that flies over the head of your average client.

2.  Exclude List Method

Good: Client doesn’t have to do anything when a new template group or page is created.

Bad: Clients with FTP privileges will wonder why their new files they have uploaded into random directories are not working.

3.  File and Directory Check Method

Good: Clients don’t have to worry about a thing when they upload new files or create templates and pages

Bad: If an image (or script or style sheet) tag references a missing file your site will end up calling an EE page, not a 404 page. This can be a massive negative impact on SEO and site performance.

The solution

The solution to this puzzle is included in the ExpressionEngine wiki although it’s not as flexible as it could be. The general idea is that the template groups list should be generated manually using an SQL query every time a new template or page is modified. Obviously not ideal.

Our solution - Introducing LG .htaccess Generator

Most of the current issues with ExpressionEngine can be squashed with a simple extension, in this case our solution is LG .htaccess Generator. LG .htaccess Generator automatically regenerates your .htaccess file every time a new page is created, a template group is modified (created, updated or deleted) or when the extension settings are saved. Simple.

Setting Up LG .htaccess Generator

The first thing you will need to do is download the LG .htaccess Generator extension from my personal site. On a side note; I’ll be moving all of my ExpressionEngine extensions, plugins and modules over to Newism as soon as I get a chance.

Once you have downloaded it, follow the instructions to install and setup the extension. One thing to remember is that you must have an existing .htaccess file already created and with write permissions in your site root.

The default .htaccess rules explained

I’ll just take a moment to explain each of the .htaccess rules in the default extension settings.

<Files .htaccess>
order allow,deny
deny from all
</Files>

Secure the .htaccess and stop malicious visitors viewing your .htaccess rules directly in the browser.

IndexIgnore *

Don’t list files in directory index pages.

ErrorDocument 404 /index.php?/{ee:404}

Point the standard 404 page to your ExpressionEngine 404 page. The {ee:404} tag will be replaced with your site’s 404 template group and template ie: site/404.

<FilesMatch "(\.jpe?g|gif|png|bmp)$">
ErrorDocument 404 "File Not Found"
</FilesMatch>

This rule overrides the previous 404 rule but just for requests that contain an image or file. It is important for site performance that a simple missing gif does not render a full ExpressionEngine page when it cannot be found.

# Remove the www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC] 
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Rewrite homepage urls that have the Google Analytics query string. More information can be found here: Using URL rewrites to make ExpressionEngine, Campaign Monitor & Google Analytics play nice.

# Rewrite homepage urls that have the Google Analyticis query string
# CampaignMonitor adds the query string in their emails
# "site" is the template group
# "index" is the template name
# The template group and template should point to your sites index
RewriteCond %{QUERY_STRING} ^utm_medium
RewriteCond %{REQUEST_URI} ^/$ [NC]
RewriteRule (.*) /index.php?/site/index/&%{QUERY_STRING} [L]

Remove the www. from the URL and redirect the request to a non www. URL with a 301 Permanently Moved header. This rule avoids Google and other search engines indexing duplicate content.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule ^(.*)$ $1/ [L,R=301]

Add a trailing slash to the URL and redirect the request with a 301 Permanently Moved header. This rule also avoids Google and other search engines indexing duplicate content.

RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5})$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/({ee:template_groups}|{ee:pages}|members|P[0-9]{2,8}) [NC]
RewriteRule (.*) /index.php?/site/index/&%{QUERY_STRING}

The grand-daddy of the .htaccess rules. This rule checks to see that the request does not end in an extension or is not an existing file. Then it checks the request to see if it contains one of our template groups or page URLs and internally redirects the request to be processed by ExpressionEngine.

The magic here is the {ee:template_groups} and {ee:pages} tags. Both of these tags will be replaced with ExpressionEngine data when the .htaccess file is generated. You should never need to manually update your .htaccess file again!

<FilesMatch "\.(html|htm|php)$">
Header set imagetoolbar "no"
</FilesMatch>

Finally we will remove that annoying IE image toolbar.

That’s about it

Well there you have it – LG .htaccess Generator – a simple to install and easy to use ExpressionEngine extension. If you use and enjoy this extension please consider sharing it with your friends on your chosen social network, bookmarking it on delicious.com or writing a blog post about it.

19/09/08 10:26am — The default settings of LG .htaccess Generator have been updated based on information in the post titled “Using URL rewrites to make ExpressionEngine, Campaign Monitor & Google Analytics play nice”.

22/09/08 9:31am — Tweaked the “remove www.” .htaccess rule.

Comments

The following 22 people were compelled to have their say. We encourage you to do the same.

  1. Adam George's Gravatar

    Adam George said on Monday 15th September, 11:36pm:

    Hi Leevi

    Great extension! I just thought you’d like to know that you have the wrong alt attribute for the Wipa logo n your footer. I saw the logo and was interested in it, which is why I noticed.

  2. Wayde Christie's Avatar

    Wayde Christie said on Tuesday 16th September, 9:58am:

    @Adam - Thanks for the tip, and glad you enjoyed the article :)

  3. giovanni's Gravatar

    giovanni said on Sunday 28th September, 7:28am:

    Hiya- i just finished installing your extension hoping that after trying all kinds of fixes that I would finally be able to remove the index.php from the urls. No luck. I still have the index.php appearing. My urls look like this: www.example.com/index.php/contact etc… I looked at the .htaccess file and it was updated by your extension but is not having the desired effect. What now?

  4. Leevi Graham's Avatar

    Leevi Graham said on Sunday 28th September, 2:41pm:

    @giovanni – The plugin only generates the required .htaccess file. It doesn’t actually remove the index.php from the ExpressionEngine generated URLs.

    To do that you will need to remove it in your system preferences. There is some more information in the wiki article which should put you on the right path.

  5. giovanni's Gravatar

    giovanni said on Tuesday 30th September, 2:09pm:

    bingo!

  6. Duo's Gravatar

    Duo said on Friday 3rd October, 5:08pm:

    Hi Leevi,

    Thanks for the great extension.  I noticed a small issue when the .htaccess is being written to.

    When I looked at the htaccess script, I noticed that it was generating an extra “|” right at the last onwer-generated template group name, which caused the entire site to display a 500 Internal Server Error.  Here’s the copied script.  When I delete the extra “|” character, everything works fine.

    RewriteCond %{REQUEST_URI} ^/(site|search|contact|News||members|P[0-9]{2,8}) [NC]

    Anything thoughts on how I can prevent this extra “|” from be written into the .htaccess file every time a new template is created?

    Thanks—you’re the best!

    duo.

  7. ryolyo's Gravatar

    ryolyo said on Saturday 4th October, 2:41pm:

    I am struggling to implement this.

    If I manually remove index.php from my urls in the browser, it works, and loads the proper templates.
    This suggests the generated htaccess file is valid
    But clicking any link loads an index.php url, so something is off. (maybe something in the EE cp?)

    mod_rewrite is enabled on my server

    I have installed the extension and double-checked the server root path.

    Set up an .htaccess file in my root with 666 permissions, and verified that the file is being written to.

    Set admin>sys prefs>gen config>”Name of your site’s index page” to blank

    The htaccess template is greek to me, and I can’t identify anything I need to edit.
    (I do have a custom name for my system directory, but I assume the ee variables take care of that.)

    Is there anything I’m missing?
    I don’t know where to go next
    (I tried posting this to the forums, but couldn’t reply for some reason)

    Thanks for any insight

  8. giovanni's Gravatar

    giovanni said on Sunday 5th October, 5:43am:

    @ryolyo see Leevi’s reply to my earleir post… edit your weblog paths in admin

  9. Leevi Graham's Gravatar

    Leevi Graham said on Sunday 5th October, 8:54pm:

    @Everyone: If you have any problems getting LG .htaccess Generator up and running head on over to the ExpressionEngine support forums and add a new post. I’ll try and answer everyone over there.

  10. ryolyo's Gravatar

    ryolyo said on Monday 6th October, 3:24am:

    You mean create a new thread?

    I can’t reply to the ongoing one
    http://expressionengine.com/forums/viewthread/90365/

  11. Jonathan Schofield's Gravatar

    Jonathan Schofield said on Saturday 3rd January, 8:32am:

    Leevi, you may find this new thread of interest regarding member activation URLs: http://expressionengine.com/forums/viewthread/101112/.

    You may have an Apache-based solution rather than my ugly hack!

  12. Tom's Gravatar

    Tom said on Thursday 12th February, 4:54am:

    I had to comment out remove IE toolbar portion otherwise the server returned a 500 error.

  13. Allan White's Gravatar

    Allan White said on Wednesday 11th March, 5:21pm:

    @Tom - I also had issues with the IE toolbar section - same as you. I’m checking the EE forum thread, but wanted to note it here.

  14. Kyle Pritchard's Gravatar

    Kyle Pritchard said on Wednesday 18th March, 6:28am:

    I had a few problems with linking my CSS files using this extension. To get around this problem add a few lines into the .htaccess rules:

    #Sort Out CSS Problems
    RewriteCond %{QUERY_STRING} ^(css=.*)$ [NC]
    RewriteRule ^(.*)$ /index.php?/%1 [L]

    Hopes this helps anyone with the same problem

  15. Peter's Gravatar

    Peter said on Monday 6th April, 5:20am:

    Awesome plugin. But I still get an internal server error. I tried a lot of things, but it still won’t work. Help me please!

  16. Luc's Gravatar

    Luc said on Monday 13th April, 6:49pm:

    Can this extension be used on a Windows server, using Helicon ISAP_Rewrite 3 and a httpd.conf file?

  17. Luc's Gravatar

    Luc said on Monday 13th April, 8:16pm:

    Got it to work with httpd.conf, by changing one line of code:

    Old: $file = $dir . “.htaccess”;
    New: $file = $dir . “httpd.conf”;

    You might add a setup-field “Name of .htaccess file” for this, so people don’t have to modify the code.

  18. vlado varbanov's Gravatar

    vlado varbanov said on Tuesday 12th May, 9:35pm:

    I have a problem with linking a javascript templates in my site - after running the LG generator. Any solution ?

  19. AlletlyZege's Gravatar

    AlletlyZege said on Wednesday 20th May, 10:50pm:

    Great page.. Will definitely come back soon.

  20. Farid's Gravatar

    Farid said on Monday 1st June, 10:11am:

    great !

  21. Tom's Gravatar

    Tom said on Wednesday 3rd June, 8:28am:

    When I remove index.php from the General Configuration the ee contact form module no longer works. The form action no longer has the index.php so it’s just posting to / and not /index.php. Any idea how to resolve?

  22. BridgingUnit's Gravatar

    BridgingUnit said on Tuesday 16th June, 10:26pm:

    If you alter the rules like so:

    RewriteCond %{QUERY_STRING} !^(ACT=.*)$ [NC]
    RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5})$
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} ^/({ee:template_groups}|{ee:pages}|members|P[0-9]{2,8}) [NC]
    RewriteRule (.*) /index.php?/site/index/&%{QUERY_STRING}

    Then logging out and any other URLs that relay on the ?ACT= string to operate should work again. At least they do for me.

Your comment

Please keep your comments friendly and on topic.

Your info
Your comment