India English
Kenya English
United Kingdom English
South Africa English
Nigeria English
United States English
United States Español
Indonesia English
Bangladesh English
Egypt العربية
Tanzania English
Ethiopia English
Uganda English
Congo - Kinshasa English
Ghana English
Côte d’Ivoire English
Zambia English
Cameroon English
Rwanda English
Germany Deutsch
France Français
Spain Català
Spain Español
Italy Italiano
Russia Русский
Japan English
Brazil Português
Brazil Português
Mexico Español
Philippines English
Pakistan English
Türkiye Türkçe
Vietnam English
Thailand English
South Korea English
Australia English
China 中文
Somalia English
Canada English
Canada Français
Netherlands Nederlands

How to set up rules and redirects in .htaccess files in Cpanel

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.

  1. Directory Access Authorization and authentication Rule
  2. Blocking Access Rules
  3. Custom Error Pages Rule
  4. Mod_Rewrite / Redirection Rule

 

Get a .co.ke domain from KSh 299

Search for your business name to see which extensions are available.

First-year price for eligible registrations.

  • 500+ domain extensions
  • Low registration and renewal prices
  • One-click push to Cloudflare
  1. 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.

  1. 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 from
192.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 from
192.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 ^(.*)$ [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] 

Start With This

Billing cycle

Best value compares each plan's lowest monthly equivalent; contract lengths can differ.

WebHosting Starter

Kshs 500/moSave 62%

Kshs 188/mo

Kshs 6,768 billed every 3 yearsSave KES 11,232

Order Now

Renews at Kshs 188/month

  • Hosts 10 Websites
  • WordPress Hosting & other CMS
  • ~25 000 visits monthly
  • Free Website Migrations
  • 30 GiB SSD Storage
  • 1-click Installer
  • Free automated SSL
  • Unlimited bandwidth
  • Unlimited Email Accounts
  • Free Daily backups
  • Free Website Builder
  • Free Website Templates
  • Python and NodeJS Support
  • Shared IP Address
  • Powered by Cpanel
See all plans

Compare hosting options for your website when you’re ready.

See plans and pricing

Leave a comment

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