Understanding .htaccess
The .htaccess file is a powerful configuration file used by the Apache web server. It allows you to control URL redirects, password protection, caching, and many other features on a per-directory basis without modifying the main server configuration.
Locating and Editing .htaccess
- Open DirectAdmin File Manager.
- Navigate to your
public_htmldirectory. - If you do not see a
.htaccessfile, ensure hidden files are visible (look for a Show Hidden Files option). - Click on
.htaccessto edit it, or create a new file named.htaccessif one does not exist.
Always back up your existing
.htaccess file before making changes. A syntax error in this file will cause a 500 Internal Server Error for your entire website.Common .htaccess Rules
Redirect HTTP to HTTPS:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Redirect www to non-www:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [L,R=301]
Password-Protect a Directory:
AuthType Basic
AuthName "Restricted Area"
AuthUserFile /home/username/.htpasswds/public_html/passwd
Require valid-user
Block an IP Address:
Deny from 192.168.1.100
Set Custom PHP Values:
php_value upload_max_filesize 64M
php_value post_max_size 64M
php_value max_execution_time 300
URL Rewriting
The mod_rewrite module enables clean, SEO-friendly URLs:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Most CMS platforms like WordPress automatically create and manage their own
.htaccess rules. If you are adding custom rules, place them above or below the CMS-generated block and do not modify the auto-generated sections.