What is .htaccess

.htaccess (with a dot at the beginning of the name) is the file that allows you to manage the server within your hosting account. If you create a .htaccess file in the root directory of your account, it will control the entire account. You can also create it in any folder of the root directory (except CGI-bin) and manage the content of these folders, each one separately.

Changes you make to .htaccess files take effect immediately and do not require a server restart.

Syntax of .htaccess

Paths to files and directories should be specified from the root of the server, for example, /pub/home/server1/html/.

Protocols (http) must be specified in domain names.

How to create a .htaccess file?

You can create the file in Notepad. Write down all lines you need and then click on “Save As”, you need to choose the file type “All Files *.* (“All Files” *.*) and in the “File Name” field write .htaccess (with a dot!). If it is not possible, save the file as .htaccess.txt, and after loading by FTP, change the name to .htaccess using an FTP client.

What is .htaccess used for?

Authorization and authentication – .htaccess files are used to create restrictions for a specific directory.
Your own error pages – change the error pages that are displayed when errors occur on the server-side.
URL changes – .htaccess files are used to change overly complex URLs into easy-to-remember ones.
Cache-control – .htaccess allows the server to control the caching of the site by web browsers to reduce bandwidth usage and server load.

.htaccess basic rules

HTTP -> HTTPS redirect
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

An alternative way to set up the redirect can be found at the link

HTTP with www -> HTTPS without www redirect
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Redirect between two different domains
RewriteEngine On 
RewriteCond %{HTTP_HOST} domain1.com 
RewriteRule (.*) http://domain2.com/$1 [R=301,L]

where domain1.com is the domain from which the redirect is made, and domain2.com is the domain to which the redirect is made

Redirect from each page of one domain to the main page of another domain
RewriteCond %{REQUEST_URI} (.*)
RewriteRule ^(.*)$ http://site.ru/ [L,R=301]
where domain2.com is the domain to which the redirect is made
Redirect from each page of one domain to the same page of another domain
RewriteCond %{REQUEST_URI} (.*)
RewriteRule ^(.*)$ http://domain2.com/$1 [L,R=301]

where domain2.com is the domain to which the redirect is made

Redirect to 403 when visiting the site
Order deny,allow
Deny from all
Block one or more IPs
Order deny,allow
allow from all
deny from IP1
deny from IP2

where instead of IP1 and IP2 you need to specify the IP address values for which you want to block access to the site.

Bot blocking
RewriteEngine on 
RewriteBase / 
RewriteCond %{HTTP_USER_AGENT} ^.*(Bot01|Bot02).*$ [NC] 
RewriteRule . -  [F,L]

where instead of Bot01, Bot02 you need to specify the real user-agent. Through the symbol “|”, you can write as many user-agents as you like, for example:

RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} ^.*(AhrefsBot|AspiegelBotBaiduspider|bingbot|binance).*$ [NC]
RewriteRule . - [F,L]
Enabling gzip compression with .htaccess

This is a standard set of rules. If necessary, you can change the set to your needs

<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/css application/json text/xml application/xml text/x-component text/javascript application/javascript application/x-javascript
Enabling cache with .htaccess

This option is suitable if you want the visitor’s browser to cache the necessary files of your site and not request them every time (because it may create a load on the site on subsequent visits).

<ifModule mod_expires.c>
ExpiresActive On
# the default expiry times - 5 seconds
ExpiresDefault "access plus 5 seconds"
# separate caching times are set for individual files type
# cache flash and images for 30 days
ExpiresByType image/x-icon "access plus 2592000 seconds"
ExpiresByType image/jpeg "access plus 2592000 seconds"
ExpiresByType image/png "access plus 2592000 seconds"
ExpiresByType image/gif "access plus 2592000 seconds"
ExpiresByType application/x-shockwave-flash "access plus 2592000 seconds"
# cache css, javascript and text files for 7 days
ExpiresByType text/css "access plus 604800 seconds"
ExpiresByType text/javascript "access plus 604800 seconds"
ExpiresByType application/javascript "access plus 604800 seconds"
ExpiresByType application/x-javascript "access plus 604800 seconds"
# cache html and htm files for 1 days
ExpiresByType text/html "access plus 43200 seconds"
# cache xml files for 10 minutse
ExpiresByType application/xhtml+xml "access plus 600 seconds"
Rate this article
Share this article
Notify of
Inline Feedbacks
View all comments
In this article