apacheconf

There are numerous reason you may wish to redirect an old webpage or file on your webserver to a new location, typically however this is used when moving web address or trying to improve your search engine ranking.

Duplication can count against you in certain search engine rankings, this includes the same document reachable from different URLs, for example http://techhack.co.uk/ and http://www.techhack.co.uk/index.php and http://www.techhack.co.uk/ and http://www.techhack.co.uk/index.php all point towards the same file, however this can be viewed from four different locations. It’s easy to see why search engines such as Google may easily pick this up and count it as duplicated information or even plagiarism.

On an Apache web server we can use a .htaccess file to instruct the server to look else where under certain conditions.

 

To create a permanent redirect (301) from an old file to a new location:

Syntax:

Redirect <redirect-type> <old-source> <new-destination>

Example -

Redirect 301 http://techhack.co.uk/oldfile.html http://techhack.co.uk/newfile.html

 

To Redirect a non-WWW domain to a WWW domain:

Syntax:

Options <follow-symbolic-links>

RewriteEngine <state>

RewriteBase <base-directory>

RewriteCond [rule-condition] [! - Not, ^ - start-of-string, NC – not-case -sensitive]

RewriteRule [rule] [L – Last Rule, R – redirect-type]

Example

Options +FollowSymLinks

RewriteEngine On

RewriteBase /

RewriteCond %{HTTP_HOST} !^www.techhack.co.uk$ [NC]

RewriteRule ^(.*)$ http://www.techhack.co.uk/$1 [L,R=301]

 

You could also use and alter the rules and conditions above to point an index file such as index.html or index.php to the root director / of a site – further cutting down on “duplicated” content.

Example

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /index.php HTTP/

RewriteRule ^index.php$ http://www.techhack.co.uk/ [R=301,L]

apacheconf

“Hot Linking” also known as “bandwidth stealing” relates to websites linking directly to non-html objects not on their own server, such as images, scripts, pdf’s etc. The victim’s server in this case is deprived of bandwidth as the hot linked file is served to a client. Meaning the offending website is able to provide content without the cost of the bandwidth.

Using a .htaccess file, you can disallow hot linking on your server.

Syntax:

RewriteEngine <state>

RewriteCond [rule-condition] [! - Not, ^ - start-of-string]

RewriteCond [rule-condition] [NC – not-case -sensitive]

RewriteRule [rule-filetypes] [F – Forbidden, $ – end-of-string]

Example – To redirect from an old file to a new location the

RewriteEngine on

RewriteCond %{HTTP_REFERER} !^$

RewriteCond %{HTTP_REFERER} !^http://(www.)?techhack.co.uk/.*$ [NC]

RewriteRule .(gif|jpg|png|js|css|pdf)$ – [F]

Note: The PHP mod_rewrite function needs to be enabled on your server in order for this aspect of .htaccess to work.

apacheconf

Apache web servers implement the use of .htaccess (hypertext access) files to govern the way information is managed for a client. These directory-level configuration files are placed inside a web tree and allow for decentralized management of a web server’s configuration, capable of overriding the server’s global configuration.

The original purpose of .htaccess was to allow per-directory access control (e.g. requiring a password to access specific content). Today .htaccess can override many other configuration settings, commonly related to content control and proves a powerful tool in the world of Search Engine Optimisation.

Some very useful and common uses of .htaccess files are listed below.

Custom Directory Index Files

You can change a default index file of directory so that a user is served a default page should they request a directory. For example if a user requests /foo/, Apache will serve up /foo/index.html

Syntax:

DirectoryIndex < default-file >

Example:

DirectoryIndex index.html index.php index.htm

Note: In the above code snippet Apache will first try to serve the file index.html should it exist, failing the existence of index.html Apache will then attempt to serve index.php and so on.

Custom Error Pages

Standard error messages can be unsupportive and worse they actually drive viewers off your site! You may therefore wish to redirect your users to a custom error page. Error message can be mapped to a specified webpage, or you may also write a common page for all the http errors as follows:

Syntax:

ErrorDocument < error-code > < location -of-custom-page>

Example:

ErrorDocument  /error.html

ErrorDocument 403 /403.html

ErrorDocument 404 /404.html

File and directory access control

.htaccess can be used to restrict access to individual files and folders.

One way to restrict user access would be by IP

Syntax:

order <setting-priority>

deny from <address>

allow from <address>

Example – Only those on the local IP 192.168.0.1 would be granted access

order deny,allow

deny from all

allow from 192.168.0.1

These types of rules become useful for filtering out undesirable IP blocks, known risks, perhaps some persistent robot that doesn’t play by the rules. In which case you would use a deny from < ip-address>

You may also wish to password protect a file. In which case you will need to create a .htpasswd file. This file stores your credentials for validating a user and should NOT be placed in a folder reachable externally.

Syntax:

AuthType <authentication-method>

AuthUserFile <passwordfile-location>

<Files <file-type>>

Require <requirement>

Example – The file test.mp3 will ask for a username and password to match that of those stored in the file C:/web-site/.htpasswd on the web server.

AuthName “Restricted Area”

AuthType Basic

AuthUserFile C:/ web-site/.htpasswd

<Files test.mp3>

require valid-user

</Files>

Modifying Environment Variables

Environment variables contain information used by the web server. Set/Unset environment variables using SetEnv and UnSetEnv.

Syntax:

SetEnv <environment-option> <option-setting>

Example – To set your website webmaster

SetEnv SITE_WEBMASTER “Robert Longworth”

Compression

Example – If your web server has the mod_gzip module enabled

<IfModule mod_gzip.c>

mod_gzip_on       Yes

mod_gzip_dechunk  Yes

mod_gzip_item_include file      .(html?|txt|css|js|php|pl)$

mod_gzip_item_include handler   ^cgi-script$

mod_gzip_item_include mime      ^text/.*

mod_gzip_item_include mime      ^application/x-javascript.*

mod_gzip_item_exclude mime      ^image/.*

mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*

</IfModule>

Example – If your web server has the mod_deflate module enabled

<Location>

SetOutputFilter DEFLATE

SetEnvIfNoCase Request_URI 

.(?:gif|jpe?g|png)$ no-gzip dont-vary

SetEnvIfNoCase Request_URI 

.(?:exe|t?gz|zip|gz2|sit|rar)$ no-gzip dont-vary

</Location>