7 Nifty htaccess Tips

Published Monday 8th of June 2009

If you use Apache you've probably heard of a file called .htaccess. It can be extremely useful for a number of different things. Here are some htaccess-tricks I've been using myself.

# A little mod rewritin
<IfModule mod_rewrite.c>
    
# Start ModRewrite
    
RewriteEngine On
    RewriteBase 
# Root-dir for your site

    # Prevent access for visitors without user-agent string (prevents some spam)
    
RewriteCond %{HTTP_USER_AGENT} = ""
    
RewriteRule .* - [F,L]

    
# Prevent www-access (you should change the yourdomain-bit)
    
RewriteCond %{HTTP_HOST} ^www\.yourdomain\.com$ [NC]
    
RewriteRule ^(.*)$ http://yourdomain\.com/$1 [R=301,L]

    # Force a trailing slash for dirs
    
RewriteRule !\..{2,4}$ - [C]
    
RewriteCond %{REQUEST_URI} !^.*/$
    
RewriteRule ^(.+)$ $1/ [R=301,L]

    
# These are useful for running everything but real files through index.php (the way many CMSes and blogs work)
    # Redirect /index.php to /
    
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php
    RewriteRule 
^index.php/?(.*)$ $[R=301,L]

    
# Run everything else but real files through index.php
    
RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond 
%{REQUEST_FILENAME} !-f
    RewriteRule 
^(.*)$ index.php/$1?%{QUERY_STRING} [L]
</
IfModule>

# These are for performance and they increase
# speed a lot! (try yslow wiv and wivout)
# Configure ETags
FileETag MTime Size

# Enable expirations
<IfModule mod_expires.c>
    
ExpiresActive On
    ExpiresDefault 
"access plus 3 days"
</IfModule>

# Gzip HTML, CSS and JS
<IfModule mod_deflate.c>
    
AddOutputFilterByType DEFLATE text/html text/css application/x-javascript text/plain text/xml
</IfModule>

Hope it wasn't all old news to you!

Tags
Comments
No comments

Bookmark this Article

  • del.icio.us
  • Digg
  • Furl
  • Google
  • Technorati
  • Ma.gnolia
  • Blinklist
  • Blogmarks
  • Rojo
  • Stumbleupon

No Comments

No comments yet, why not be the first to post one?

Post a Comment

Random jQuery Plug-ins

  • Togglable DL

    Use this plug-in on your definition-lists (dl-elements) to allow the dd:s for a dt to be toggled when clicking the dt. Don't use it unless it's semant...

    Details

  • View More

    This plug-in allows an element's contents to be hidden untill the user clicks a certain element. It works exactly like the HTML5 details and summary e...

    Details

  • Static to Dynamic Google Map

    This plug-in turns a static google map into a dynamic one by inspecting the attributes passed to the static map API. It's still in beta and doesn't su...

    Details

More Plug-ins

Recent Comments

  1. Andreas on "SleekPHP User Handling"

    Thank you for showing interest :) Were the docs en...

  2. ciptard on "SleekPHP User Handling"

    Ok, thank you

  3. Andreas on "SleekPHP User Handling"

    I might write a "getting started" article for Slee...