UnderHost
Knowledgebase Docs

Advanced .htaccess Configuration in DirectAdmin

Configure .htaccess files in DirectAdmin for redirects, security, performance, PHP settings. URL rewriting, HTTPS, access control, caching.

On this page

.htaccess is a per-directory Apache configuration file that controls rewriting, redirects, security, PHP settings, and caching. DirectAdmin (Apache) treats .htaccess like cPanel does. Editable via File Manager or SFTP. Changes take effect immediately.

What is .htaccess?

  • Per-directory config: Controls behavior of Apache in that folder and below
  • Requires AllowOverride: Server admin must enable .htaccess in Apache config
  • Immediate effect: No server restart needed
  • Performance cost: Apache reads it on every request; keep rules minimal

Find and Edit .htaccess

  1. Log in to DirectAdmin
  2. Go to File Manager
  3. Navigate to your domain: /domains/yourdomain.com/public_html/
  4. Enable Show Hidden Files (checkbox)
  5. Click .htaccess to edit, or right-click → Edit
  6. Save changes (reloads immediately)

Via SFTP: Connect to your server, navigate to public_html/, enable hidden files in your SFTP client, edit .htaccess with a text editor, and upload.

Force HTTPS

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

What it does: Any HTTP request redirects to HTTPS with a permanent (301) redirect. Add at the top of .htaccess after any existing RewriteEngine directives.

URL Rewriting

# Remove .html extension
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.html [NC,L]

# Redirect old URLs to new URLs
RewriteRule ^old-page\.html$ /new-page/ [R=301,L]

# WordPress-style rewriting (for apps)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?q=$1 [QSA,L]

Security Rules

# Block access to sensitive files

  Order allow,deny
  Deny from all


# Block access to directories

  Order allow,deny
  Deny from all


# Block bad bots
RewriteCond %{HTTP_USER_AGENT} (bot|crawler|spider) [NC]
RewriteRule .* - [F]

PHP Configuration

# Increase upload limit
php_value upload_max_filesize 64M
php_value post_max_size 64M

# Increase execution time
php_value max_execution_time 120

# Disable directory listing
Options -Indexes

# Set default file
DirectoryIndex index.php index.html

Browser Caching

# Cache static files for 30 days

  Header set Cache-Control "max-age=2592000, public"


# Don't cache HTML (pages update)

  Header set Cache-Control "no-cache, must-revalidate"

Troubleshooting

500 Internal Server Error: .htaccess syntax error. Check Apache error log at /var/log/apache2/error_log. Fix syntax and remove invalid directives.

Rules not working: Ensure RewriteEngine is ON and appear before any rewrite rules. Check that AllowOverride is enabled on the server (contact support if not).

Slow site: Many rewrite rules slow down Apache. Optimize rules, move complex logic to application code, or use caching plugins instead.

Syntax errors break your site

A single invalid line in .htaccess returns 500 errors for the entire directory. Test carefully, and keep a backup of the working version.

Related: cPanel .htaccess | Redirect guide | Apache config

Was this article helpful?

Need DirectAdmin hosting?

DirectAdmin is available for customers who want a lightweight hosting control panel on compatible UnderHost services.

Back to DirectAdmin