How to redirect between non-www and www URL in Apache
Websites are typically hosted on a URL that starts with or without www. The choice could be personal, though sometimes are required. Certain CDN, for example, requires a website to be hosted on a subdomain (www) to work.
Either way, you can configure your Apache to work on both with and without www and then automatically redirect your visitor from one to the other.
You can automatically redirect between www and non-www (also known as naked domain) versions of your website and vice versa using .htaccess file or Redirect directive in Apache's configuration file. You can configure redirection from the dashboard if you're hosting on cPanel.
Methods to redirect domain and URL in Apache:
Redirect naked domain to www in Apache using htaccess
-
Enable rewrite module for Apache.
$ sudo a2enmod rewrite # Ubuntu, Debian and SUSE variants Enabling module rewrite. To activate the new configuration, you need to run: systemctl restart apache2
-
Distribution with a2enmod support can simply run the command above without having to manually enable the required modules.
-
CentOS and Red Hat enables the module by default so requires no manual action to enable the modules.
Options Debian, Ubuntu openSUSE and SLES Fedora Core, CentOS, RHEL macOS homebrew xampp a2enmod support yes yes no no no no Modules to install none Module name n/a rewrite Loadmodule directive n/a LoadModule rewrite_module <module_locations>/mod_rewrite.so -
-
Open or create a .htaccess file on the web folder where you want to set the redirection from.
$ sudo vi /var/www/html/.htaccess
-
Add redirect directive from within the .htaccess file.
RewriteEngine On RewriteCond %{HTTP_HOST} ^!simplified.guide$ [NC] RewriteRule ^(.*)$ http://onlineweb.tools/$1 [R=301,L]
301 is equivalent to permanent redirect and you can use 302 instead for temporary redirect.
-
Hard-reload the web page to test the redirect.
Redirect naked domain to www in Apache using Virtualhost
-
Enable rewrite module for Apache.
$ sudo a2enmod rewrite # Ubuntu, Debian and SUSE variants Enabling module rewrite. To activate the new configuration, you need to run: systemctl restart apache2
-
Distribution with a2enmod support can simply run the command above without having to manually enable the required modules.
-
CentOS and Red Hat enables the module by default so requires no manual action to enable the modules.
Options Debian, Ubuntu openSUSE and SLES Fedora Core, CentOS, RHEL macOS homebrew xampp a2enmod support yes yes no no no no Modules to install none Module name n/a rewrite Loadmodule directive n/a LoadModule rewrite_module <module_locations>/mod_rewrite.so -
-
Open VirtualHost config that you want to set up the redirection from using your favorite text editor.
$ sudo vi /etc/apache2/sites-enabled/000-default.conf
-
Add redirect directive within VirtualHost configuration.
<VirtualHost *:80> ServerName simplified.guide Redirect permanent / http://onlineweb.tools/ </VirtualHost>
permanent is equivalent to 301 redirect and you can use temporary instead for 302 redirect.
-
Restart Apache for the changes to take effect.
Related: How to manage Apache service
Guide compatibility:
Operating System |
---|
Ubuntu 16.04 LTS (Xenial Xerus) |
Ubuntu 16.10 (Yakkety Yak) |
Ubuntu 17.04 (Zesty Zapus) |
Ubuntu 17.10 (Artful Aardvark) |
Ubuntu 18.04 LTS (Bionic Beaver) |
Ubuntu 18.10 (Cosmic Cuttlefish) |
Ubuntu 19.04 (Disco Dingo) |