Websites are normally hosted on a domain name that started with www (www.simplified.guide for example), though some prefer hosting it on a non-www or naked domain (a domain without the leading www or any other subdomain).
One way or another, you can configure your Apache to work on both and then force your visitor to use the one of your choice via automatic redirection.
An example would be to redirect visitors of simplified.guide to www.simplified.guide.
This can be done using Apache whether using the Redirect directive in your Apache's VirtualHost configuration or by using htaccess file.
Methods to redirect domain and URL in Apache:
$ sudo a2enmod rewrite # Ubuntu, Debian and SUSE variants Enabling module rewrite. To activate the new configuration, you need to run: systemctl restart apache2
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 |
$ sudo vi /var/www/html/.htaccess
RewriteEngine On RewriteCond %{HTTP_HOST} ^!simplified.guide$ [NC] RewriteRule ^(.*)$ http://www.simplified.guide/$1 [R=301,L]
301 is equivalent to permanent redirect and you can use 302 instead for temporary redirect.
$ sudo a2enmod rewrite # Ubuntu, Debian and SUSE variants Enabling module rewrite. To activate the new configuration, you need to run: systemctl restart apache2
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 |
$ sudo vi /etc/apache2/sites-enabled/000-default.conf
<VirtualHost *:80> ServerName simplified.guide Redirect permanent / http://www.simplified.guide/ </VirtualHost>
permanent is equivalent to 301 redirect and you can use temporary instead for 302 redirect.
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) |
Comment anonymously. Login not required.