Warning: Table './db79366_designmess/sessions' is marked as crashed and should be repaired query: SELECT u.*, s.* FROM users u INNER JOIN sessions s ON u.uid = s.uid WHERE s.sid = '15c9adbf56f05cc73f1f1f6b550d18ab' in /nfs/c05/h04/mnt/79366/domains/designmess.com/html/includes/database.mysqli.inc on line 128

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /nfs/c05/h04/mnt/79366/domains/designmess.com/html/includes/database.mysqli.inc:128) in /nfs/c05/h04/mnt/79366/domains/designmess.com/html/includes/bootstrap.inc on line 1037

Warning: Cannot modify header information - headers already sent by (output started at /nfs/c05/h04/mnt/79366/domains/designmess.com/html/includes/database.mysqli.inc:128) in /nfs/c05/h04/mnt/79366/domains/designmess.com/html/includes/bootstrap.inc on line 636

Warning: Cannot modify header information - headers already sent by (output started at /nfs/c05/h04/mnt/79366/domains/designmess.com/html/includes/database.mysqli.inc:128) in /nfs/c05/h04/mnt/79366/domains/designmess.com/html/includes/bootstrap.inc on line 637

Warning: Cannot modify header information - headers already sent by (output started at /nfs/c05/h04/mnt/79366/domains/designmess.com/html/includes/database.mysqli.inc:128) in /nfs/c05/h04/mnt/79366/domains/designmess.com/html/includes/bootstrap.inc on line 638

Warning: Cannot modify header information - headers already sent by (output started at /nfs/c05/h04/mnt/79366/domains/designmess.com/html/includes/database.mysqli.inc:128) in /nfs/c05/h04/mnt/79366/domains/designmess.com/html/includes/bootstrap.inc on line 639
Snippets and Resources to get started with .htaccess | [field_tutorial_category-term] | Designmess

Snippets and Resources to get started with .htaccess

Submitted by on

.htaccess files (or Hypertext Access files) are configuration files that run on your Apache server. They are very powerful, and extremely useful in a variety of projects. You can do things like prevent hotlinking, redirecting, special error pages, password protect directories, and even redirect iPhone users. .htaccess can make a huge impact on your next project, so here are some useful resources, tools, techniques, articles.

Getting Started

In order to use htaccess, you need to create a .htaccess file. This will only work on a Apache web server, but most hosts use Apache, so chances are you will be fine. If you are unsure, you should get in touch with your host and ask them if they support .htaccess files.
Once you find out that your web server supports .htaccess, start by creating a blank file in a text editor of your choice. Name this file ".htaccess" (without the quotes), and upload it to the root directory of your web server.
Congratulations! You now have a .htaccess file to modify!

Snippets

Here are some simple ways to make a big difference by only adding a few lines to your .htaccess file! Open up that .htaccess file with an editor of your choice and simply copy and paste the snippets below. Make modifications to them when you need to, and finally, upload the .htaccess file to the server!

Custom Error Pages

Users receive an error page if they type a url wrong, a link redirects them to a invalid URL, or the server is down. Replacing the standard (and boring) error page on your server is easy. By adding a few lines to our .htaccess file, we can redirect users to custom error pages.
Before
A default 404 file not found page.

  1. ErrorDocument 401 /error/401.php
  2. ErrorDocument 403 /error/403.php
  3. ErrorDocument 404 /error/404.php
  4. ErrorDocument 500 /error/500.php

Now, simply create a directory named "error" and place the respective error pages inside.

After
Now you can create custom error pages like the one found at Pattern Tap

Prevent Hotlinking

If some of you keep a blog, resource site, or host your own images, music, and other files, then you may be interested in preventing hotlinking. By preventing hotlinking, you can save that valuable bandwidth and server resources. Simply post this snippet in your .htaccess file.

Hotlink
Now when someone links to your image, they will get something like this!

  1. RewriteEngine On
  2. #Replace ?mysite\.com/ with your own url
  3. RewriteCond %{HTTP_REFERER} !^http://(.+\.)?mysite\.com/ [NC]
  4. RewriteCond %{HTTP_REFERER} !^$
  5. #Replace /images/do-not-hotlink.png with your "don't hotlink" image url
  6. RewriteRule .*\.(jpe?g|gif|bmp|png)$ /images/do-not-hotlink.png [L]

You can easily change this snippet to suit your own needs. First off, change the URL to your own domain. Next, you can specify your own URL for the image users will see when they attempt to hotlink. Place this image inside the images directory, or a directory of your choice.
Also, you can prevent hotlinking to other file types by changing the file extension specified.

You can also prevent hotlinking from specific domains. Just use this snippet instead.

  1. RewriteEngine On
  2. #Replace with your own urls
  3. RewriteCond %{HTTP_REFERER} ^http://(.+\.)?myspace\.com/ [NC,OR]
  4. RewriteCond %{HTTP_REFERER} ^http://(.+\.)?blogspot\.com/ [NC,OR]
  5. RewriteCond %{HTTP_REFERER} ^http://(.+\.)?livejournal\.com/ [NC]
  6. RewriteRule .*\.(jpe?g|gif|bmp|png)$ /images/do-not-hotlink.png [L]
Here is a helpful tool that helps you prevent hotlinking.

Compress your site with gzip

Most browsers these days claim to have gzip capabilities, which means you can make your site load up to 75% faster by compression your files automatically with gzip.

Before
Yahoo uses gzip compression on their home page, compressing their page by 90%!

Adding this snippet to your .htaccess file will check if the browser supports gzip, and then compress all your web files.

  1. <ifModule mod_gzip.c>
  2. mod_gzip_on Yes
  3. mod_gzip_dechunk Yes
  4. mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$
  5. mod_gzip_item_include handler ^cgi-script$
  6. mod_gzip_item_include mime ^text/.*
  7. mod_gzip_item_include mime ^application/x-javascript.*
  8. mod_gzip_item_exclude mime ^image/.*
  9. mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
  10. </ifModule>

Force File Download

Adding this to your .htaccess file will force a "Save As" dialog on the file types you specify.

  1. # Change file extension to your own needs
  2. AddType application/octet-stream .avi
  3. AddType application/octet-stream .mpg
  4. AddType application/octet-stream .wmv
  5. AddType application/octet-stream .mp3

Secure Connection

You can redirect your visitor over a secure https connection by adding these few lines.

  1. RewriteEngine On
  2. RewriteCond %{HTTPS} !on
  3. RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

Paypal
Paypal uses a secure https connection.

Change PHP upload limits

Override your server's default maximum upload limits with this quick snippet. This will allow you to upload bigger files that you normally couldn't. Be sure to check with your host before doing this, as it may not work on some servers.

  1. php_value upload_max_filesize 20M
  2. php_value post_max_size 20M
  3. php_value max_execution_time 200
  4. php_value max_input_time 200

With that code, the limit is 20MB, but you can modify that to suit your needs.

Protect Directory View

This one liner will prevent visitors to see your directory if they do not have a index file.

  1. Options All -Indexes

Before
Sample default directory showing all the files inside that directory.

After
Adding the snippet will not allow visitors to see inside the directory.

Redirect to new domain

If you have switched to a different domain, you can redirect the visitors that go to your old domain.

  1. # redirect from old domain to new domain
  2. RewriteEngine On
  3. RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L]

Change Default Page

You can set the default page from the regular index.html to something else with this quick snippet.

  1. # serve alternate default index page
  2. DirectoryIndex something-else.html

Redirect Visitors while in Maintenance

If you are performing site maintenance or updating, you can redirect users to a page of your choice.

  1. order deny,allow
  2. deny from all
  3. allow from 123.123.123.123
  4.  
  5. ErrorDocument 403 /maintenance.html
  6.  
  7. <Files maintenance.html>
  8. allow from all
  9. </Files>

Be sure to replace "123.123.123.123" with your own IP address.
Check to see what your IP address is.

Twitter
Whenever Twitter is undergoing maintenance, they display this page.

Better URLs

Clean up and make your URLs easier to remember and more user friendly.

  1. RewriteEngine on
  2. RewriteRule ^about/$ /pages/about.html [L]
  3. RewriteRule ^news/$ /news.php [L]
  4. RewriteRule ^contact/$ /pages/contact.htm [L]

Redirect users to a new page

If you have recently changed a page, and want to redirect users to the new one, check out this snippet below.

  1. Redirect 301 /old.html http://mydomain.com/new.html

Redirect iPhone Users

You can use htaccess to redirect users visiting with the iPhone.

  1. RewriteEngine on
  2. RewriteCond ${HTTP_USER_AGENT} iPhone
  3. RewriteRule .* http://mydomain.com/iphone

Block Spammers and other unwanted visitors

If you know the IP address of a spammer, you can block them with a few lines of code.

  1. Order Deny,Allow
  2. Deny from 123.123.123.123

Remember to replace the IP address in the snippet with the IP address of the spammer.

Secure the .htaccess file

One of the last things to do is to secure your .htaccess file, and prevent users from viewing it.

  1. <Files .htaccess>
  2. order deny,allow
  3. deny from all
  4. </Files>

Tutorials and Resources

If you are interested in looking more in depth about .htaccess and what it can do, below are some tutorials, articles, and other resources related to .htaccess.

Ultimate guide to htaccess files


Extensive and very in depth guide on using htaccess understanding how it works.

5 Fun and Practical .htaccess solutions

Five easy and practical solutions using .htaccess

Do's and Don'ts of .htaccess


A dozen techniques to get you started

Htaccess Tricks for your Wordpress Blog


Neat tricks to improve your wordpress blog.

Htaccess Password Protection Tricks


Easy ways to protect files and directories.

CSS File Protection


Learn how to protect your CSS files from rippers and other bad people.

Htaccess Password Generator


A script that will generate .htaccess and .htpasswd files to protect directories.

Enjoy this article? Be sure to follow us on twitter.

Internet Nerd, Design Freak, oh and Designmess Admin.

About Sean GengMember Since 08/03/2009

Web Designer, Web Developer

My name is Sean Geng. I'm a Freelance Web Designer, Graphic Designer, Motion Effects Artist, and Webmaster. I like to push the boundaries. I love creating unique, clean, usable design for the web and other digital sources.

CommentsAdd your comment

Great article Sean, I've just started playing around with htaccess, mostly for creating neat URLS but will be putting some more of this into action shortly. Keep up the good work!

February 25, 2010 - 7:18am

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Lines and paragraphs break automatically.
  • Allowed HTML tags: <h3> <h4> <i> <b> <p> <a> <br /> <img> <em> <strong> <blockquote> <cite> <code> <ul> <ol> <li> <div class="info-block-left"> <div class="info-block-right"> <div class="quote-left"> <div class="quote-right"> <div class="notice"> <table> <thead> <tbody> <tr class="odd"> <tr class="even"> <tr> <td> <th>
  • You may post PHP code. You should include <?php ?> tags.
  • Web page addresses and e-mail addresses turn into links automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <actionscript-code>, <actionscript3-code>, <apache-code>, <asp-code>, <css-code>, <html-code>, <javascript-code>, <mysql-code>, <php-code>, <python-code>, <ruby-code>, <sql-code>, <xml-code>. The supported tag styles are: <foo>, [foo].
  • Images can be added to this post.
  • Image links with 'rel="lightbox"' in the <a> tag will appear in a Lightbox when clicked on.
  • Links to HTML content with 'rel="lightframe"' in the <a> tag will appear in a Lightbox when clicked on.
  • Each email address will be obfuscated in a human readable fashion or (if JavaScript is enabled) replaced with a spamproof clickable link.
  • You may insert videos with [video:URL]

More information about formatting options

Other stories you might like

Sorry if its gonna be slow content wise for a week or so. Working on a free wordpress theme. Consider writing for us?