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
01 Answer
1) Check the module rewrite this enabled:
sudo a2enmod rewrite
sudo service apache2 restart2) 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]