[SOLVED] Virtualhosts problem

I have a domain whose file are located at /var/www/vhosts/example.com/htdocs

When I try to access it with www.example.com, it opens fine. But when I try to access it without "www" it shows the contents of /var/www.

Any idea why?

Here's the content of /etc/apache2/sites-available/example.com

 <virtualhost *="">ServerAdmin admin@example.com
ServerName example.com
ServerAlias www.example.com

DocumentRoot /var/www/vhosts/example.com/htdocs

AddHandler fcgid-script .php
FCGIWrapper /usr/lib/cgi-bin/php5 .php

LogLevel warn
ErrorLog /var/www/vhosts/example.com/log/error.log
CustomLog /var/www/vhosts/example.com/log/access.log combined</virtualhost> 

7 Replies

edit misread the question

I think I've found the problem, but not the solution. My hostname is set to "example.com" so the default configuration file in /etc/apache2/sites-available interferes with /etc/apache2/sites-available/example.com.

Here's the default file:````
NameVirtualHost *
ServerAdmin webmaster@localhost
DocumentRoot /var/www

 <directory>Options  Indexes FollowSymLinks MultiViews
    AllowOverride all</directory> 
     <directory var="" www="">Options Indexes FollowSymLinks MultiViews
    #Options -FollowSymLinks
    AllowOverride all
    Order allow,deny
    allow from all
    # This directive allows us to have apache2's default start page
            # in /apache2-default/, but still have / go to the right place
           # RedirectMatch ^/$ /apache2-default/</directory> 

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <directory "="" usr="" lib="" cgi-bin"="">AllowOverride all
    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
    Order allow,deny
    Allow from all</directory> 

ErrorLog /var/log/apache2/error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn

CustomLog /var/log/apache2/access.log combined
ServerSignature On

Alias /doc/ "/usr/share/doc/"
<directory "="" usr="" share="" doc="">Options Indexes MultiViews FollowSymLinks
    AllowOverride None
    Order deny,allow
    Deny from all
    Allow from 127.0.0.0/255.0.0.0 ::1/128</directory></virtualhost> 

````

What should I change to make example.com (without www) work?

1) Are you sure you want your hostname set to example.com ?

2) Do you have DNS A records for both "www.example.com" and "example.com" pointing to your IP ?

3) Alternatively, remove the ServerAlias directive and create a second virtualhost setup for "example.com", with an .htaccess redirect to "www.example.com" (or vice versa). This is probably safer in the long term.

Thanks for the reply.

1) What else should I set it? I mean I have several domains there and should I set it to something that's not a domain? That would cause problems with spam blacklists because I send mail from these domains. Any suggestions?

2) Yes, I have them as they are automatically added by Linode's DNS manager when set as "master".

3) This seems like an ugly workaround, if there's an alternative I'd like to go with that.

–----

Now, I've changed my hostname to something that doesn't make sense, like "whatever.com". This solved all my problems, but this is surely not the way to go. What can I do?

Ok, problem solved. I've created an A record for "server.example.com" and I've set my hostname to server.example.com as well. Now, there's no configuration file for server.example.com in /etcapache2/sites-available/, so the default config doesn't cause problems with any other config files.

Thanks for the help mjrich.

@dcelasun:

1) What else should I set it? I mean I have several domains there and should I set it to something that's not a domain? That would cause problems with spam blacklists because I send mail from these domains. Any suggestions?
The hostname needn't be related to mail – use mailname (or some derivative if more than one domain).
> 3) This seems like an ugly workaround, if there's an alternative I'd like to go with that.
Creating a second virtual host, redirecting to the first is the most reliable option (though I'd be interested if others have a more developed opinion). To paraphrase a faq at NFS, using both the bare domain and the www version without redirects creates issues such as:
* Noncanonical URLs for your site. It is desirable to have one URL for each page, for purposes of bookmarking, etc. Having multiple valid URLs for the same page makes your site less popular in rankings and for search engines.

  • Potentially less reliable. DNS CNAME records cannot be used in this situation. You can put in a couple of A records, but this is still not as effective for load balancing and fault tolerance as doing it the "right" way.

  • It becomes hopelessly confusing if you have (or might ever have) more than one web site under the domain.

  • See also http://no-www.org

    > Now, I've changed my hostname to something that doesn't make sense, like "whatever.com".
    From your example, it seems as if you're trying to use a FQDN as a hostname, but surely this isn't the case (?)

Glad to hear it's all working though :)

Currently, it is set to "server.example.com", which is an FQDN, so everything is fine.

I'll also set an htaccess redirect for www, I now understand your points. Thanks for pointing them out!

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct