Techie Weblog

Web Hosting & Network Security Guide

HostGator Web Hosting

How to Write .htaccess file for Web Server

If you like the post, Please Share

Facebooktwittergoogle_pluspinterestlinkedin

How to Write .htaccess file for Web Server

How to create a .htaccess file - Web Hosting

How to create a .htaccess file – Web Hosting

You may have heard about .htaccess file for web server configuration. If you have installed any Content Management System(CMS) such as WordPress, you must have encountered with that file. This file plays an important role in website SEO. The .htaccess file has so many features which includes the ability to protect folders with password, ban users or allow users using IP addresses, stop directory listings, redirect users to another page or directory automatically. Besides, it can create and use custom error pages, change the way files with certain extensions are utilized, or even use a different file as the index file by specifying the file extension or specific file. In this article I will discuss about the importance and how to write a .htaccess file for web server. But you should remember that .htaccess file can slow your server.

What is .htaccess file?

.htaccess stands for HyperText ACCESS. It a configuration file which is widely implemented in Apache Based Web Servers around the world. The file name starts with a period (.) followed by htaccess. The .htaccess file is detected and executed by the Apache Web Server software. It can be used to alter the configuration of the Apache Web Server software to enable/disable additional functionality and features that the Apache Web Server software has to offer. These facilities include basic redirect functionality, the instance is given below:

  • 400 – Bad request
  • 401 – Authorization Required
  • 403 – Forbidden
  • 404 – File Not Found
  • 500 – Internal Server Error

 

How to write .htaccess file.

You can write it using any editor, even with Notepad.

For redirection of domain name (Domain name Canonicalization):

If you are using yourdomain.com and www.yourdomain.com both domain, then you are going to face “Canonicalization” problem. Canonicalization is the process of picking the best URL when there are several choices, and it usually refers to home pages. Technically yourdomain.com and www.yourdomain.com are different. A web server could return completely different content for all the urls above. When search engines “canonicalizes” a url, we try to pick the best url between them. But since www is the preferred one, so you have to redirect yourdomain.com to www.yourdomain.com. This can be done by the following code. Just change the yourdomain.com with your domain name.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^yourdomain\.com
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [L,R=301]

For redirection of IP ( IP Canonicalization):

If your IP address is 165.158.21.15 and your preferred domain is www.yourdomain.com, then there is chance that search engines index both of them and your webpage rank affected adversely. So, you have to choose either of them. If you want to redirect your IP address to your preferred domain then append the following code in you .htaccess file.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^165\.158\.21\.15
RewriteRule (.*) http://www.yourdomain.com/$1 [R=301,L]

For denying Visitors by  IP address:

This IP Address Blocking feature facilitates us to deny access to specific visitors, or allow access to specific visitors. This is extremely useful for blocking unwanted visitors, or to only allow the web site owner access to certain sections of the web site, such as an administration area.

order allow,deny
deny from 255.0.0.0
deny from 139.41.32.
allow from all

Above piece of code will block the IP address 255.0.0.0 and the addresses those matches with 139.41.32, i.e., from IP address 139.41.32.0 to 139.41.32.255.

For denying Visitors by  Referrers:

This feature facilitates us to deny access to visitors from specific referrers, or allow access to visitors from specific referrers. It is very useful for blocking unwanted visitors from a referrer you don’t like, or to only allow the visitors from specific referrers. Copy this piece of code and append it.

RewriteEngine on
# Options +FollowSymlinks
RewriteCond %{HTTP_REFERER} disliked-domain\.com [NC]
RewriteRule .* – [F]

For Custom page redirection:

If you want to make error pages look friendlier and to provide more information to the site visitor than the default server error page offers, you can use the .htaccess file to create custom error pages like this.

ErrorDocument 404 /error404.html

ErrorDocument 500 /error500.html

For compression of web pages:

Compression??? Yes you are right, compression can be enabled by adding some code to .htaccess on the web server. Just add the following code and speed up download time and save bandwidth as well.

<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>

If the above code does not work then please try the following codes.

AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript

For setting Time-To-Live for CSS, Image files:

There is a way to set the TTL in for certain resources, like CSS and/or JS files. Append the following codes to do that. The code is self explanatory.

<ifModule mod_headers.c>
<ifModule mod_expires.c>
# images for 1 hour
<filesMatch “\.(jpeg|jpg|png|gif|flv|pdf|swf)$”>
ExpiresActive On
ExpiresDefault “access plus 1 year”
Header set Cache-Control “public, max-age=31536000, must-revalidate”
</filesMatch>
# CSS and js for longer
<filesMatch “\.(ico|css|js|eot|ttf|ttc|otf|woff)$”>
ExpiresActive On
ExpiresDefault “access plus 365 days”
Header set Cache-Control “private, max-age=31536000, must-revalidate”
</filesMatch>
</ifModule>
</ifModule>

Sample .htaccess file

# Redirect IP to Non-WWW to WWW
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^165\.158\.21\.15
RewriteRule (.*) http://www.yourdomain.com/$1 [R=301,L]

RewriteEngine on
RewriteCond %{HTTP_HOST} ^yourdomain\.com
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [L,R=301]

RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* – [F,L]
# END Redirect IP to Non-WWW to WWW

# BEGIN GZIP
# mod_gzip compression (legacy, Apache 1.3)
<IfModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file \.(html?|xml|txt|css|js)$
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>
# END GZIP

# DEFLATE compression
<IfModule mod_deflate.c>
# Set compression for: html,txt,xml,js,css
AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml application/xhtml+xml text/javascript text/css application/x-javascript
# Deactivate compression for buggy browsers
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4.0[678] no-gzip
BrowserMatch bMSIE !no-gzip !gzip-only-text/html
# Set header information for proxies
Header append Vary User-Agent
</IfModule>
# END DEFLATE
<ifModule mod_headers.c>
<ifModule mod_expires.c>
# images for 1 hour
<filesMatch “\.(jpeg|jpg|png|gif|flv|pdf|swf)$”>
ExpiresActive On
ExpiresDefault “access plus 1 year”
Header set Cache-Control “public, max-age=31536000, must-revalidate”
</filesMatch>
# CSS and js for longer
<filesMatch “\.(ico|css|js|eot|ttf|ttc|otf|woff)$”>
ExpiresActive On
ExpiresDefault “access plus 365 days”
Header set Cache-Control “private, max-age=31536000, must-revalidate”
</filesMatch>
</ifModule>
</ifModule>

# do not use ETag
FileETag None

 

*** How to Write .htaccess file for Web Server ***

Updated: November 7, 2015 — 5:41 pm

The Author

Sukanta Dutta

The author is writing technical blog for last few years. He shares his knowledge on Computer Networks, Database Technologies, Security Aspects of Network and Database etc. He also likes to hear from the reader of this blog to learn more, so he welcomes guest writing for this blog.

Leave a Reply

Your email address will not be published. Required fields are marked *

Techie Weblog © 2015-2018