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
- Log in to cPanel and open File Manager.
- Navigate to public_html.
- If you cannot see .htaccess, click Settings at the top right and tick Show Hidden Files, then click Save.
- Right-click .htaccess and click Edit.
- 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.
