Celeb Glow
general | March 20, 2026

How can I make apache redirect to www subdomain?

I have the following in sites-available

<VirtualHost *:80> DocumentRoot /var/www/html/mywrk DirectoryIndex index.php ServerName <Directory "/var/www/html/mywrk"> Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all Require all granted </Directory> </VirtualHost>

Here it's working but mydomain.com (without www) does not redirect to

In the apache2 host, how can I set non-www to redirect to www? I mean mydomain.com should redirect to

0

1 Answer

1) Check the module rewrite this enabled:

sudo a2enmod rewrite
sudo service apache2 restart

2) In the main configuration of Apache you must have something like this enabled:

/etc/apache2/sites-available/default
<Directory /var/www/html> Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all
</Directory>

Note the this line: AllowOverride All

3) Then, drop the following into a file called .htaccess in you root directory server

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]
RewriteRule ^(.*)$ [L,R=301]

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy