Our optimised ExpressionEngine htaccess file – Web Performance

The htaccess file is one of the smallest on any linux powered website yet it's the most powerful. Many problems can arise from an htaccess file. If you are having any redirect or problem with your ExpressionEngine urls then it's likely that the htaccess file could be that gremlin you need to fix.

#--- BEGIN EXPIRES for web performance
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresDefault "access plus 10 days"
    ExpiresByType text/css "access plus 1 week"
    ExpiresByType text/plain "access plus 1 month"
    ExpiresByType image/gif "access plus 1 month"
    ExpiresByType image/png "access plus 1 month"
    ExpiresByType image/jpeg "access plus 1 month"
    ExpiresByType application/x-javascript "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 week"
    ExpiresByType application/x-icon "access plus 1 year"
#---Remove Etags -  for web performance
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)(\.gz)?$">
Header unset ETag
FileETag None
#---Gzip for web performance
<ifmodule mod_deflate.c>
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript text/javascript
#---sometimes you need to add video type for them to play properly
AddType video/mp4 .mp4
AddType video/mp4 .mov
AddType video/quicktime .mov
#--- activates rewrite engine
RewriteEngine on
#--- take away slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)/$  /$1 [R=301,L]
#--- Remove index.php from url if not an image
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ /index.php/$1 [L]  
#--- When home page is requested use index.php – rather than index.htm
DirectoryIndex index.php 
#--- remove index.php from root url  
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ http://www.mediasurgery.co.uk/$1 [R=301,L] 
#--- make sure www is there
RewriteCond %{HTTP_HOST} ^mediasurgery.co.uk
RewriteRule (.*) http://www.mediasurgery.co.uk/$1 [R=301,L]
#--- redirect from index.php - redirected after removing in the 4th step above
RewriteCond %{THE_REQUEST} ^[^/]*/index\.php [NC]
RewriteCond %{THE_REQUEST} ^GET
RewriteRule ^index\.php(.+) $1 [R=301,L]
#--- redirect old page to new page 
Redirect 301 /page-to-redirect http://www.mediasurgery.co.uk/page-to-redirect-to


The redirects need to be in that order for them to work. Altering what part of the redirect is read by the server and the order can affect results greatly.

At Media Surgery we take web performance very seriously and a lot of the above htaccess is for just that - performance. Gzipping, Expires and Etags can have a significant effect on page load time and activating them is good practice and a good idea.

With this technique of using the redirect you will need to put an "RewriteEngine off" in an another htaccess file in your system folder to stop that from redirecting and not being able to access the control panel.

SEO: Canonical issues in search engine optimisation can arise from having both www. And non www. based urls hence why we have redirected all pages to have a url starting with www.

SEO: Slashes in urls can also have search engine effects. Removing or forcing slashes is again good practice and will help with canonical issues. In this case we are removing the trailing slash at the end of every url.

SEO: The final line redirects older pages that have moved to a new location. This again helps if you have links pointing at older pages and want the 'juice' to be transferred to that new page.