What does htaccess stand for?

This file is used to set up the web site without changing the server configuration.

Creation of htaccess file

You can build an htaccess file using text editing software via the command line or via the file manager in cPanel.

Examples for htaccess

1. To redirect a domain permanently

If domain “abc1.com” need to be redirected to “abc2.com”, you may use the following code in htaccess file.

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

2. Redirect site to www or without www

a. Example: to load “www.racknerd.com” in the browser:

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

b. Example: to load “racknerd.com” in the browser:

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

3. For blocking IPs

We may limit access to our domain’s IP addresses by using the code in htaccess mentioned below.

Order Deny,Allow
Deny from X.X.X.X (where X.X.X.X is the said IPv4 address)

For blocking several IP addresses:

Order Deny,Allow
Deny from X.X.X.X
Deny from Y.Y.Y.Y

In order to restrict access from specific countries, you should obtain the IP addresses that are assigned to that particular country through third-party sites such as https://lite.ip2location.com

4. To alter the default homepage

You can change the default home page of your domain to load with the desired php file using the code

DirectoryIndex file.php

5. To load your domain securely (Force SSL with htaccess)

You must first install an SSL certificate for the domain and then add the code below to your . htaccess file.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ – [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{ENV:HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

Server Hosting Solutions by RackNerd:

Shared Hosting
cPanel Web Hosting in US, Europe, and Asia datacenters
Reseller Hosting
Create your new income stream today with a reseller account
VPS (Virtual Private Server)
Fast and Affordable VPS services - Instantly Deployed
Dedicated Servers
Bare-metal servers, ideal for the performance-demanding use case.

Leave a comment

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