How to Create And Use the .htaccess File

The .htaccess (Hypertext Access) file is a configuration file placed in your website’s root folder. It tells the server how to handle specific requests. Most websites use it for URL rewriting, security rules, caching, and redirects.

Common use cases include:

  • Creating SEO-friendly URLs
  • Redirecting pages (301 redirects)
  • Blocking IP addresses
  • Setting custom error pages
  • Enabling compression for faster loading

It’s easy to create and edit, but must be handled carefully because wrong rules can break your site.

How to Create a .htaccess File

You can create a .htaccess file in just a few steps:

Step 1: Open a Text Editor

Use any basic editor:

  • Notepad (Windows)
  • TextEdit (Mac)

Step 2: Save the File

Save the file as:

.htaccess

Make sure:

  • There is no file extension
  • It starts with a dot (.)

Step 3: Upload the File to Your Server

Using:

  • cPanel File Manager
  • FTP (FileZilla)
  • Hosting dashboard

Upload it to the public_html or root folder of your domain.

Popular .htaccess Rules You Can Use

1. 301 Redirect (Permanent Redirect)

Used when moving pages or domains:

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

2. Force HTTPS

For secure browsing:

RewriteEngine On

RewriteCond %{HTTPS} off

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

3. Create SEO-Friendly URLs

Remove index.php or file extensions:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . /index.php [L]

4. Block a Specific IP

Order Allow,Deny

Deny from 123.456.789.000

Allow from all

5. Custom Error Pages

ErrorDocument 404 /404.html

ErrorDocument 403 /403.html

Why the .htaccess File Is Important

The file helps you:

Improve SEO

Clean URLs, redirects, and error pages help with indexing.

Improve Website Security

You can block bad bots, disable directory browsing, and protect files.

Speed Up the Website

Enable cache control and compression (Gzip).

Manage Redirects Easily

Fix broken links or moved pages with simple rules.

Enhance User Experience

Custom error pages and HTTPS improve trust.

Best Practices for Using .htaccess

  • Always take a backup before editing.
  • Test after making any changes.
  • Avoid unnecessary rules to prevent slow performance.
  • Use proper redirect types (301, 302).
  • Keep your file clean and organized.
Scroll to Top