.htaccess Redirect Guide on DirectAdmin

The .htaccess file is the backbone of URL management on Apache Web Server — which powers every DirectAdmin hosting plan. Whether you need to redirect old pages to new URLs, force HTTPS, or clean up your URL structure, this guide covers every .htaccess redirect method with copy-paste-ready code examples.

Editing .htaccess on DirectAdmin

There are two main ways to edit .htaccess on DirectAdmin:

  1. File Manager: Go to DirectAdmin → Files → File Manager → public_html → click .htaccess → Edit
  2. FTP: Connect via FileZilla and edit the .htaccess file in the domain root directory

Important: .htaccess is a hidden file (dot file) on Linux. If using FTP, enable "Show hidden files" first. In FileZilla, go to Server → Force showing hidden files.

301 vs 302 Redirect: Key Differences

Understanding the difference is essential before choosing a redirect type:

Redirect HTTP to HTTPS

The most critical redirect in modern web hosting — forcing all traffic to use HTTPS:

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

For servers behind a proxy or load balancer, use the forwarded protocol header instead:

RewriteEngine On RewriteCond %{HTTP:X-Forwarded-Proto} !https RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Redirect www to non-www (and vice versa)

www → non-www (recommended for most sites)

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

non-www → www

RewriteEngine On RewriteCond %{HTTP_HOST} !^www\. [NC] RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Redirect a Specific Page

For redirecting individual pages to new URLs, the simpler Redirect directive works well:

# Permanent redirect (301) Redirect 301 /about-us.html /about.html # Temporary redirect (302) Redirect 302 /sale /promotion.html

Redirect an Entire Folder

Redirect all URLs under an old folder to a new folder:

RedirectMatch 301 ^/old-blog/(.*)$ /blog/$1

Custom Error Pages with .htaccess

Beyond redirects, .htaccess also lets you define custom error pages — giving visitors a well-designed experience instead of Apache's raw error output:

# Define custom error pages ErrorDocument 404 /404.html ErrorDocument 403 /403.html ErrorDocument 500 /500.html # Or redirect to a URL ErrorDocument 404 https://yourdomain.com/not-found

A well-crafted 404 page is especially important: when visitors land on a broken link, a good custom 404 page keeps them on your site by offering navigation links, a search bar, or links to popular content. Without it, Apache displays a plain error that most visitors immediately leave.

SEO tip: Your custom 404 page must return an actual HTTP 404 status code — not 200 OK. A "soft 404" (200 status on a not-found page) can confuse Googlebot and cause it to index error pages as real content.

Query String Redirects

Redirect URLs with specific query string parameters — useful when migrating from dynamic URLs to clean static slugs:

RewriteEngine On # Redirect ?id=123 to clean URL RewriteCond %{QUERY_STRING} ^id=123$ RewriteRule ^product\.php$ /products/new-product/? [L,R=301] # Strip all query strings from a path RewriteCond %{QUERY_STRING} .+ RewriteRule ^shop/(.*)$ /shop/$1? [L,R=301]

Note the trailing ? in the target URL — this tells Apache to strip the original query string from the redirect destination, preventing duplicate parameters from being appended.

Redirect vs RewriteRule: Which to Use?

Both directives handle redirects, but they differ in power and use case:

Feature Redirect Directive RewriteRule
Complexity Simple, single URL Flexible, supports patterns
Regex support No Full regex support
Conditional logic Not available RewriteCond conditions
Example Redirect 301 /old /new RewriteRule ^old(.*)$ /new$1 [L,R=301]
Best for Single page redirects HTTPS, www, folder, pattern redirects

Trailing Slash Redirects

Force trailing slash

RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_URI} !/$ RewriteRule ^(.*)$ /$1/ [L,R=301]

Remove trailing slash

RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)/$ /$1 [L,R=301]

Redirect Old Domain to New Domain

When migrating your entire site to a new domain:

RewriteEngine On RewriteCond %{HTTP_HOST} ^(www\.)?old-domain\.com$ [NC] RewriteRule ^(.*)$ https://new-domain.com/$1 [L,R=301]

Preventing Redirect Loops

Redirect loops (ERR_TOO_MANY_REDIRECTS) are the most common .htaccess mistake. Prevent them by:

Testing Redirects with curl and Online Tools

After editing .htaccess, test redirects immediately from the command line or use an online checker to view the full redirect chain:

# Check HTTP headers (single hop) curl -I http://yourdomain.com # Follow all redirects and show each hop's headers curl -IL http://yourdomain.com

Online tools like httpstatus.io or redirect-checker.org display the full redirect chain with each step's status code — making it easy to spot loops or unnecessary hops. Keep your redirect chain to two hops or fewer (for example, http → https in one hop, not http → http://www → https). Each extra hop adds network latency for every visitor.

Complete .htaccess Template for Most Sites

Options -Indexes RewriteEngine On # 1. Force HTTPS RewriteCond %{HTTPS} off RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] # 2. Redirect www → non-www RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC] RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301]

Tip: All AsiaGB hosting plans support mod_rewrite and AllowOverride All, so .htaccess works at full capacity. Plans start from 500 THB/year with SSD storage, DirectAdmin control panel, and PHP 7.4/8.2/8.3 support.

SSD Hosting with DirectAdmin

Full mod_rewrite support on all plans. Configure .htaccess exactly as you need. Starting at 500 THB/year with 99% Uptime.

View Hosting Plans

View all cheap Thailand web hosting plans →