Tealmermaid Designs

web design, coding, and SEO tips and tricks

Codes for .htaccess

Share:

htaccess codes


An .htaccess file is a short (or not) file on your web site which exists to 'direct traffic', so to speak. When another server attempts to access any file on your site, the b>.htaccess file determines how to direct that server to locate the file it seeks.

Begin by creating a simple text file named .htaccess (note the beginning period). In this file you will code all the redirects needed for your site. In the absence of any code in your .htaccess file about a particular file on your server, the server will serve the requested file.




Redirection Header codes

The code for a moved or missing file has the form Redirect XXX /filename_1 /filename_2. This is useful if you delete a file, rename a file, or move a file.

Key:

Redirect 301 /projects_old.php /projects.php

Redirect to an error page

If your page has moved or been removed, you can use an .htaccess file to have your server serve an error page. The code for a error page redirect has the form ErrorDocument XXX /error-page.php. Although other codes exists, there are four primary error codes you are most likely to use for this.

Key:

ErrorDocument 404 /404-error.php

Hotlink allow or deny

If your referrer logs are showing that other sites are hotlinking your images, you will want to use your .htaccess to deal with this. The simplest methods are to either forbidden access entirely or respond by serving an image that indicates access to that file is not allowed.

You can code your .htaccess your server allow access to certain sites including your own.

Key:

The code for a allow/deny hotlink redirect which returns a Forbidden error has the form
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^!^http(s)?://(www\.)?yoursite\.com.*$ [X]
RewriteRule .*\.(filetypes)$ - [F]

'Not a blank referrer, not one of the allowed non case sensitive domains, if one of these file types is requested serve a Forbidden error.'

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?tealmermaid\.com.*$ [NC]
RewriteRule .*\.(gif|jpg|png|jpeg|css|flv)$ - [F]

The code for a allow/deny hotlink redirect which returns an image has the form
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^!^http(s)?://(www\.)?yoursite\.com.*$ [X]
RewriteRule .*\.(filetypes)$ http://yoursite.com/nohotlink.jpe [XXX]
.

'Not a blank referrer, not one of the allowed non case sensitive domains, serve a no-hotlink image, Last Rule.'

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?tealmermaid\.com.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?pinterest.com.*$ [NC]
RewriteRule .*\.(gif|jpg|png|jpeg|css|flv)$ http://tealmermaid.com/nohotlink.jpe [NC,R,L]

Note that if you choose to serve a 'no hotlink' graphic, it will have to be a file type other than the types not listed. I recommend changing the file type for this file to be something that this both unusual yet web-safe such as jpe. This can be done with any graphics software.