Welcome to Webmaster HTML

.htaccess Examples and Tips & Tricks

Good Evening!

The .htaccess file is used at the directory level to configure and manipulate files on Apache servers for various actions such as url redirects, cache control, security, canonical change and other functions. It is usually recommended to put these directives in the httpd.conf instead of the .htaccess file for performance reasons but some may not have access to their httpd.conf file. .htaccess files can be put in more than one directory to change the characteristics of files in those other directories.

Web Developer Resources

.htaccess examples

These are some examples of configuration directives that can be put in .htaccess files. You will need to change the domain or file names. If the directive does not work, it may throw a 500 error on your website – do not panic. Simply remove the .htaccess file from the server and start over. Many times its simply a misspelling in the directive but sometimes it is because Apache is not set up to use certain configuration directives. It is a matter of turning on or installing that service.

Cache :

# Best one so far https://css-tricks.com/snippets/htaccess/set-expires/
<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"

# Another good cache method http://www.askapache.com/htaccess/speed-up-sites-with-htaccess-caching.html
<ifModule mod_expires.c>
ExpiresActive On
ExpiresDefault A300
ExpiresByType image/x-icon A2592000
ExpiresByType application/x-javascript A3600
ExpiresByType text/css A3600
ExpiresByType image/gif A604800
ExpiresByType image/png A604800
ExpiresByType image/jpeg A604800
ExpiresByType text/plain A300
ExpiresByType application/x-shockwave-flash A604800
ExpiresByType video/x-flv A604800
ExpiresByType application/pdf A604800
ExpiresByType text/html A300

Block Domain Names, IP Addresses, directory access, Framing Etc.:

# Block Domain Names
RewriteEngine on
RewriteCond %{HTTP_REFERER} ^http://.*FirstDomain\.com/ [NC,OR]
RewriteCond %{HTTP_REFERER} ^http://.*DomainWithDash\-for\-website\.com/ [NC,OR]
RewriteCond %{HTTP_REFERER} ^http://.*MiddleDomains\.com/ [NC,OR]
RewriteCond %{HTTP_REFERER} ^http://.*LastDomain\.com/ [NC]
RewriteRule ^(.*)$ – [F,L]

# Block IP Addresses
order allow,deny
deny from
deny from 123.45.6.
allow from all

# Prevents framing of your website into another website
Header always append X-Frame-Options SAMEORIGIN

# Use this to protect a directory by putting this .htaccess in that directory. It blocks all IP addresses except the ones you allow here. You can use a single IP or a range. This example allows only these IPs
order deny,allow
deny from all
allow from

# Prevent directory listing
Options -Indexes

A few redirects :

# Redirect Entire Directory
RedirectMatch 301 http://www.then.com/old/directory/(.*) http://www.now.com/new/directory/$1 [R=301,L]

# Redirects from old directory to new directory
Redirect /old_dir/ http://www.example.com/new_dir/index.html

# Redirects a file to a different domain
Redirect 301 /oldfile.php http://example.net/newfile.php

# Redirect old domain to a another domain
RewriteEngine on
RewriteCond %{HTTP_HOST} ^old.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.old.com [NC]
RewriteRule ^(.*)$ http://new.com/$1 [L,R=301,NC]

Canonical Redirects:

# Redirects to www. version of your domain
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301,NC]

# Redirects to non www. version of your domain
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301,NC]