Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

Redirect www to non-www (or the Other Way) With .htaccess in DirectAdmin

Your site can be reached as www.example.com and example.com. This guide shows how to send visitors to just one version.

DirectAdmin2 min read11 steps4 screenshots

Why do this?

To a person both addresses show the same site. But search engines may treat them as two sites. This can split your ranking. A redirect is a signpost that sends visitors from one address to another. Pick one version and send everything there.

The .htaccess file is a small settings file for the Apache web server. The dot at the start means it is hidden. It affects the folder it lives in.

Steps

  1. Log in to DirectAdmin.
  2. Open File Manager under System Info and Files.
  3. Go to your site's folder, usually domains/example.com/public_html.
  4. Look for .htaccess. If you cannot see it, enable the option to show hidden files.
  5. Make a backup. Copy the file and name it .htaccess-backup.
  6. Click the .htaccess file and choose Edit. If there is no file, create a new one with that name.
    Screenshot: Click the .htaccess file and choose Edit . If there is no file, create a new one with that
  7. Add one of the blocks below at the very top. Do not remove existing lines.
  8. Click Save.

Send non-www to www

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

This sends visitors of example.com to www.example.com.

Send www to non-www

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

This does the reverse. Replace example.com with your own domain in each line.

Test it

  1. Clear your browser cache, or use a private window.
  2. Type the address you want to redirect.
  3. Check that the browser bar changes to the other version.

If you see a 500 error, restore the backup. A typo is the usual cause. If you see "too many redirects", your site or WordPress may be forcing the opposite version. Make the site address in WordPress match your choice.

Tip: The code uses https. Your site needs an SSL certificate first. SSL is the padlock that scrambles data. If you do not have one, use http in the rule.

Quick recap

  • Choose www or non-www and send everything there.
    Screenshot: Choose www or non-www and send everything there.
  • Edit .htaccess in File Manager after backing it up.
    Screenshot: Edit .htaccess in File Manager after backing it up.
  • Paste the right block and swap in your domain.
  • Test in a private window. Restore the backup if the site breaks.
    Screenshot: Test in a private window. Restore the backup if the site breaks.