Mobile app version of vmapp.org
Login or Join
Karen161

: Redirect https://example.com naked to https://www.example.com I am redirecting all http:// request to https:// using apache2 virtual host file: <VirtualHost *:80> ServerName example.com

@Karen161

Posted in: #301Redirect #Apache2 #Https #NoWww #Virtualhost

I am redirecting all request to using apache2 virtual host file:

<VirtualHost *:80>
ServerName example.com
ServerAlias example.com Redirect / www.example.com/ </VirtualHost>

<VirtualHost *:443>
ServerName example.com #more details here
</VirtualHost>


This is working fine and redirecting all request to , but now I want to redirect my non-www request to I tried to add these lines before and after the above <VirtualHost *:443> with no success.

<VirtualHost *:443>
ServerName example.com
Redirect / www.example.com/ </VirtualHost>


How can I make it redirect from non-www to www whilst enforcing SSL in the virtual host file?

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Karen161

1 Comments

Sorted by latest first Latest Oldest Best

 

@Alves908

Try something like this:

<VirtualHost *:80>
ServerName example.com
ServerAlias example.com DocumentRoot "/path/to/port/80/site"
</VirtualHost>

<VirtualHost *:443>
ServerName example.com
ServerAlias example.com DocumentRoot "/path/to/port/443/site"
</VirtualHost>


Create an .htaccess file in /path/to/port/80/site with:

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


Create an .htaccess file in /path/to/port/443/site with:

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

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme