0

I have applied ServerName and ServerAlias in my vhost and from here it is mentioned that Servername is hostname and port that server used to identify itself while ServerAlias is the alternate names for a host. Now my website is works in https+www.example.com, so I've set my

ServerName as "www.example.com" and my ServerAlias put it as "example.com"(Previously was comment out, now I tried to apply it back)

so that when I type "example.com" in the browser, it will redirect me to "https://www.example.com" (please correct me if I understand it wrongly). But even after I apply those, it still doesn't redirect me, do I need to flushdns and stop-start my DNS? Because there are a few times that all changes have been done, restart apache and nothing reflects until I flushdns, is this a must? Thanks in advance!

1

I cannot leave comments because I'm new but you should look at https://httpd.apache.org/docs/2.4/mod/core.html#usecanonicalname

I am assuming you are using Apache and name based virtualhosts

Again, sorry I couldn't leave this as a comment.

8
  • Adding a comment to test if you can comment down below, can you try add a comment below?
    – Petri
    Apr 22 '20 at 2:23
  • Yeah, if you reply I can comment. Apr 22 '20 at 2:29
  • I did try to add UseCanonicalName but with On instead because I have an issue where my logs showing rewritecond input= 'www.example.com' but when I type 'example.com' in the browser(which I expect it to redirect from example.com to https+www.example.com) but it doesn't because of host of request header showing 'example.com' instead. What should I do now?
    – Petri
    Apr 22 '20 at 2:31
  • @Petri, hmm, not sure. Let me get this straight, you want the server to use the URL as passed by the browser example.com and www.example.com but you don't want one redirecting to the other. Apr 22 '20 at 2:33
  • @Petri You might be able to use .htaccess to rewrite requests for example.com to http:// example.com. Not sure what your logs record a rewrite. You have a <virtualhost> for example.com or is it the default server? Apr 22 '20 at 2:37
1

The ServerName and all hostnames in the ServerAlias are treated the same i.e. the same configuration applies to all the hostnames specified in both directives on the <VirtualHost> section. The only difference is that the ServerName is used as identification, and cannot therefore contain more than a single hostname. That's why there's a separate ServerAlias directive for the additional hostnames. That's not an obvious solution, but that's how Apache has decided to arrange this. E.g. Nginx handles this differently, having a single server_name directive that can have multiple hostnames.

There's no automatic redirection from ServerAlias to ServerName, but you'd need to configure it e.g. by using a separate <VirtualHost> section. This example redirects all example.com & *.example.com to canonical www.example.com.

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

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

Notice that the ServerName example.com doesn't imply that example.com is the canonical name, as there a redirection from the domain apex to the www subdomain.

3
  • Let's say I have VirtualHost A with ServerName example.com now(only have one now), if I want to redirect *.example.com I need to add another new VirtualHost B below with the rest I suppose is the same configuration right?
    – Petri
    Apr 23 '20 at 8:28
  • That's one way to do it, and I think it's the most simple and straightforward solution. Apr 23 '20 at 8:41
  • I'm sorry but another stupid question from me, if my current live website is https+www and I need to redirect from example.com(actually example.com is not existed, just a request that when user type example.com in the browser, it will go to current live site) to my current live site, I still need to add VirtualHost B too or I can just put ServerName www.example.com and ServerAlias as example.com will be good enough? (I just need it to redirect from example.com to https+www.example.com)
    – Petri
    Apr 24 '20 at 8:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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