3

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

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

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

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

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

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

5
  • Why do you want to do it without htaccess?
    – Steve
    Jun 21, 2015 at 2:15
  • @Steve Why do you want to do it with htaccess?
    – MrWhite
    Jun 21, 2015 at 15:06
  • I would have expected the above to have worked. What does "no success" mean? What actually happens?
    – MrWhite
    Jun 21, 2015 at 15:06
  • @w3d I don't suggest doing it with htaccess necessarily, the OP said "without" I was curious why. - oh, I can see the question has been edited to remove that :P
    – Steve
    Jun 22, 2015 at 6:09
  • I did it with .htaccess file. curious to know is there any way to do within sites .conf file itself
    – Riyas Kp
    Jul 31, 2015 at 1:56

1 Answer 1

1

Try something like this:

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

<VirtualHost *:443>
    ServerName example.com
    ServerAlias www.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 ^(.*)$ https://www.example.com/$1 [R=301,L]

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

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
4
  • Thank you for your answer. I did it with .htaccess file. curious to know is there any way to do with in sites .conf file itself.
    – Riyas Kp
    Jul 31, 2015 at 1:57
  • @RiyasKpYiideveloper Yes there is though I have to admit not ever doing that- not able to provide a good example. Using the .htaccess file is actually better for a number of reasons the primary of which is that you do not have to restart Apache and can make changes on the fly. There is chatter that using the .htaccess is inefficient, however, that has not been true for almost 2 decades! I have been at this since the earliest days of the Internet and I always use the .htaccess file without exception.
    – closetnoc
    Jul 31, 2015 at 2:04
  • I am also using .htaccess.but read Redirect is better than Rewrite
    – Riyas Kp
    Jul 31, 2015 at 2:08
  • @RiyasKpYiideveloper Redirect can be used in .htaccess. It is thought by some that one is more efficient than the other, however, the difference is virtually nothing. It is important to know that the .htaccess file is cached and any redirect or rewrite is handled exactly the same way. With faster CPU's and the amount of memory available today, it is almost laughable to argue that there is a difference- Who cares?? It is infinitesimal. There are bigger fish to fry!
    – closetnoc
    Jul 31, 2015 at 2:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.