How to Configure .htaccess Rules

The .htaccess file is a powerful Apache web server configuration file stored in your public_html folder. It controls how your website handles URLs, security, and access rules.

How to Edit .htaccess

  1. Log in to cPanel and open File Manager.
  2. Navigate to public_html.
  3. If you cannot see .htaccess, click Settings at the top right and tick Show Hidden Files, then click Save.
  4. Right-click .htaccess and click Edit.
  5. Make your changes carefully. Click Save Changes.

Important: Always copy the full contents of .htaccess and save them somewhere safe before editing. A single syntax error breaks your entire website.

Common .htaccess Rules and What They Do

1. Force HTTPS (Redirect HTTP to HTTPS)

RewriteEngine On

RewriteCond %{HTTPS} off

RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

2. Redirect www to non-www (or vice versa)

To redirect www.yourdomain.com to yourdomain.com:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www\.yourdomain\.com [NC]

RewriteRule ^(.*)$ https://yourdomain.com/$1 [L,R=301]

3. Redirect an Old Page to a New URL

Redirect 301 /old-page/ https://yourdomain.com/new-page/

4. Block a Specific IP Address

Order Allow,Deny

Deny from 123.45.67.89

Allow from all

5. Prevent Directory Listing

Options -Indexes

6. Protect wp-config.php (WordPress)

<files wp-config.php>

order allow,deny

deny from all

</files>

WordPress and .htaccess

WordPress uses .htaccess for its permalink structure. If you change your permalink settings in WordPress (Settings > Permalinks), WordPress regenerates the .htaccess file automatically. You can also regenerate it manually by simply saving the Permalinks settings page.

Tip: Use an online .htaccess validator to check your rules before saving them to your live server.

 

  • cPanel, URL redirect, Apache, configuration, htaccess, rewrite
  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

How to Register a Domain in 5 Minutes

Getting online starts with a great domain name. Follow these simple steps to register your domain...

How to Change Nameservers of Your Domain

Changing your domain’s nameservers is essential when pointing your domain to a different hosting...

Uploading Files Using File Manager in cPanel

Uploading your website files is an essential step in going live. With cPanel's File Manager, you...

How to Access cPanel

cPanel is the control panel for your Hordanso shared hosting account. It is where you manage your...

How to Create an Email Account in cPanel

Creating a professional email address for your domain (e.g., [email protected]) takes less...

Powered by WHMCompleteSolution