What are all of the options to access only the www version of a URL e.g. https://www.example.com. The website in question has 150 pages and we need to ensure all traffic resolves to the www version.
Example: All options for this URL https://example.com to resolve to htts://www.example.com.
I’ve used an .htaccess redirect in the past, but the question is are there any other options and which is the best option
You have a tag of Apache
, so assume you are running a reasonably recent version of Apache.
You can handle it with Apache Virtual Hosts. You will set up two files:
/etc/apache2/sites-available/www.example.com.conf
<VirtualHost *:80>
ServerName www.example.com
DocumentRoot /var/www/...
...
</VirtualHost>
/etc/apache2/sites-available/example.com.conf
<VirtualHost *:80>
ServerName example.com
Redirect permanent / https://www.example.com/
...
</VirtualHost>
In Apache you then need to enable both sites:
a2ensite www.example.com
a2ensite example.com
service apache2 reload
The first file sets up the main website: www.example.com
, which directs Apache to load the DocumentRoot
directory when it comes in.
The second file indicates that when example.com
comes in on Port 80 - redirect to the https://www.example.com
. Be sure to set the http
or https
depending on whether it is secure.
This kind of redirect causes the browser to change the address in the browser bar, which is the best redirect for what you are looking to do.