How to redirect a Non WWW domain name to WWW

I am using wordpress, I have installed LAMP, created virtual host and migraated a website from other host consists www, but here in Digital Ocean I have failed to bring the www by all means. May be something I am missing. Please someone please guide me step by step.

Show comments

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Site Moderator
Site Moderator badge
October 7, 2023

Hello @mushfiq

The DNS seems sorted, so the only thing left to do is to handle the redirect itself. In Apache, this mostly happens in the .htaccess file, but you also need to ensure that Mod Rewrite is enabled.

First, to enable the mod_rewrite module you need this command:

  1. sudo a2enmod rewrite

With the Rewrite module enabled, we can configure Apache with redirect rules using .htaccess files.

You can use the .htaccess file rule provided by KFSys.

Hope that this helps

Site Moderator
Site Moderator badge
October 5, 2023


To redirect a non-www domain name to a www domain in a WordPress setup on a LAMP stack, you’d typically make modifications to the Apache configuration or to the .htaccess file within your WordPress directory.

I’ll recommend adding the 301 Redirect in your .htaccess file rather than in the Apache configuration.

  1. Backup: Before making any changes, it’s always a good practice to backup your current .htaccess file.

  2. Access .htaccess File: Navigate to the root directory of your WordPress installation, where you should find the .htaccess file.

cd /path/to/your/wordpress/directory

Open the .htaccess file

sudo nano .htaccess
  1. Add Redirect Rules: At the beginning of the .htaccess file, add the following lines:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^ [NC]
RewriteRule ^(.*)$$1 [L,R=301]

Replace with your domain name.

  1. Reload Apache: After you’ve done the changes and have saved the file, reload apache:
service apache2 restart
  1. Update WordPress Address Settings (Optional but Recommended): Ensure your WordPress Address and Site Address are consistent with your desired URL structure.
  • Login to your WordPress dashboard.
  • Navigate to Settings > General.
  • Make sure the “WordPress Address (URL)” and “Site Address (URL)” both include the www.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel