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.
- Directory Access Authorization and authentication Rule
- Blocking Access Rules
- Custom Error Pages Rule
- 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. 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 ^(.*)$ [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
Best value compares each plan's lowest monthly equivalent; contract lengths can differ.
WebHosting Starter
Kshs 188/mo
Kshs 6,768 billed every 3 yearsSave KES 11,232
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
Explore web hosting
Compare hosting options for your website when you’re ready.
See plans and pricing
Domain SearchInstantly check and register your preferred domain name
Web Hosting
cPanel HostingHosting powered by cPanel (Most user friendly)
WordPress HostingHost your WordPress website with fast, secure, and affordable WordPress hosting in Kenya.
KE Domains
Reseller HostingStart your own hosting business without tech hustles
Windows HostingOptimized for Windows-based applications and sites.
Free Domain
Affiliate ProgramEarn commissions by referring customers to our platforms
Free HostingTest our SSD Hosting for free, for life (1GB storage)
Domain TransferMove your domain to us with zero downtime and full control
All DomainsBrowse and register domain extensions from around the world
.Com Domain
WhoisLook up domain ownership, expiry dates, and registrar information
VPS Hosting
Managed VPSNon techy? Opt for fully managed VPS server
Dedicated ServersEnjoy unmatched power and control with your own physical server.