Htaccess file is a special text file with .htaccess extension that is created within apache web server to configure and set up a custom directory level server environment without changing global setting of the server.
There are several rules that can be set using .htaccess.
- Directory Access Authorization and authentication Rule
- Blocking Access Rules
- Custom Error Pages Rule
- Mod_Rewrite / Redirection Rule
- 1. Directory Access Authorization and authentication
Allow imposing of security restriction to access of directory and sub directories. When security restriction is in place, a visitor is required to provide username and password for access.
Setting up security restriction.
- Create the directory/ folder you want to protect in the following location.
/home/cpanel_user/.htpasswds/
e.g. for public_html/cloudpap the path will be .htpasswds/public_html/cloudpap /
2. Add a passwd file in the directory –(/.htpasswds/public_html/cloudpap /) and add hashed access details using this tool – http://www.htaccesstools.com/htpasswd-generator/
3. Include the following lines to .htaccess: file
AuthType Basic
AuthName “Cloudpap”
AuthUserFile /home/cpanel_user/.htpasswds/public_html/passwd
require valid-user
2. Blocking Access Rules
Allow a site administrator to prevent certain people from accessing certain sections of the website.
Blocking rules are mostly implemented using ip address.
To block 192.168.1.56 from accessing current directory using .htaccess – add the following lines to the .htaccess file
order allow,deny
allow from all
deny from192.168.1.56
To block everyone else except 192.168.1.56 from accessing current directory add the following lines on the .htaccess file.
order deny,allow
deny from all
allow from192.168.1.56
3. Custom Error Pages Rule
Custom error pages rule allow site administrator to redirect visitors to a custom page when an error occurs so that the can easily navigate to other area of the site.
Some of the errors include 404 error, 500, 403 error among others
Just ensure to create a file using the website theme with the respective error as the file name.
The add the following line to .htaccess file
ErrorDocument 404 /404.html – for 404 errors
ErrorDocument 403 /403.html– for 403 errors
ErrorDocument 500 /500.html– for 500 errors
4. Mod_Rewrite / Redirection Rule
Specifies how web page url will be displayed to the site visitors. It implements temporarily and permanent redirections.
Permanent (301) Redirect –
Most common redirect, inaddition to redirecting it directs search engines through user agents or browser that the previous url has permanently moved and therefore they should update their databses as well.
301 redirect from truehost.cloud to cloudpap.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^truehost\.cloud$ [OR]
RewriteCond %{HTTP_HOST} ^www\.truehost\.cloud$
RewriteRule ^/?$ “http\:\/\/cloudpap\.com\/” [R=301,L]
Redirect from truehost.cloud/subfolder to cloudpap.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^truehost\.cloud$ [OR]
RewriteCond %{HTTP_HOST} ^www\.truehost\.cloud$
RewriteRule ^subfolder$ “http\:\/\/cloudpap\.com\/” [R=301,L]
Temporary Redirect (302)
Most recommended type of redirect not in the cases that the former url has changed permanently.
302 redirect redirect the visitor or search engine to a new url but requires the browser or user agent not to cache or change from the old url.
302 redirect from truehost.cloud to cloudpap.com
Temporary redirect from example.com to domain.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^truehost\.cloud$ [OR]
RewriteCond %{HTTP_HOST} ^www\.truehost\.cloud$
RewriteRule ^/?$ “http\:\/\/cloudpap\.com\/” [R=302,L]
Redirect from truehost.cloud/subfolder to cloudpap.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^truehost\.cloud$ [OR]
RewriteCond %{HTTP_HOST} ^www\.truehost\.cloud$
RewriteRule ^subfolder$ “http\:\/\/cloudpap\.com\/” [R=301,L]
Other redirects performed on .htaccess
Redirect from HTTP to HTTPS
for a domain e.g. , truehost.cloud
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule .* https://truehost.cloud/%{REQUEST_URI} [R,L]
or
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^truehost\.cloud$ [OR]
RewriteCond %{HTTP_HOST} ^www\.truehost\.cloud$
RewriteRule ^(.*)$ https://www.truehost.co.ke/$1 [R,L]
Redirect from non-WWW to WWW
for any domain within the server .htaccess takes effect on:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
for a certain domain, e.g. truehost.cloud:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^truehost\.cloud$ [NC]
RewriteRule ^(.*)$ http://www.truehost.cloud/$1 [R=301,L]
Redirect from WWW to non-WWW
for any domain within the server .htaccess takes effect on:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
for a certain domain, e.g. truehost.cloud:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.truehost\.cloud [NC]
RewriteRule (.*) http://truehost.cloud/$1 [R=301,L]
Changes the directory root for the main domain (truehost.cloud) to public_html/subfolder
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?truehost.cloud$
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subfolder/$1
RewriteCond %{HTTP_HOST} ^(www.)?truehost.cloud$
RewriteRule ^(/)?$ subfolder/index.php [L]