StarDomain

.htaccess Guide — Common Rules

.htaccess Guide — Common Rules

The .htaccess file controls Apache web server behavior for your site.

Important Notes

  • The file is named .htaccess (starts with a dot — it is a hidden file)
  • It goes in your public_html directory
  • Always backup before editing
  • A syntax error can take your site offline

Common Rules

Redirect HTTP to HTTPS

apache
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Redirect www to non-www

apache
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

Block IP Address

apache
<RequireAll>
  Require all granted
  Require not ip 123.456.789.0
</RequireAll>

Custom Error Pages

apache
ErrorDocument 404 /errors/404.html
ErrorDocument 500 /errors/500.html

Password Protect a Directory

apache
AuthType Basic
AuthName "Restricted Area"
AuthUserFile /home/username/.htpasswd
Require valid-user

Increase Upload Limit

apache
php_value upload_max_filesize 64M
php_value post_max_size 64M

Enable GZIP Compression

apache
<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/html text/css text/javascript application/javascript
</IfModule>

Troubleshooting

  • 500 Error after edit: Undo changes or check syntax
  • Rules not working: Ensure mod_rewrite is enabled
  • File not visible: Enable "Show Hidden Files" in File Manager