0

I understand how to use Directory and (I think) VirtualHost directives. I further understand that I can't mix them (although I can nest them). When I mix them I get 404 not found for the Directory-type URLs that used to work.

How, then, can I support browsing both URLs such as "localhost/my/website" and also "mydomainname.com" in my config file?

My rationale: I want to make mydomainname.com use https while keeping my other websites using http. I already have mydomainname.com translating to 127.0.0.1 in my HOSTS file (my local DNS), so I think I must be close to a solution.

I've been working on this for hours and not really getting anywhere. The Apache documentation is not helping me.

Ideally, someone could sketch for me how to achieve what I want.

1 Answers1

0

Based on the question I think you don't understand the difference between Directory and VirtualHost.

<Directory> and </Directory> are used to enclose a group of directives that will apply only to the named directory, sub-directories of that directory, and the files within the respective directories.

<VirtualHost> and </VirtualHost> are used to enclose a group of directives that will apply only to a particular virtual host. Any directive that is allowed in a virtual host context may be used. When the server receives a request for a document on a particular virtual host, it uses the configuration directives enclosed in the <VirtualHost> section.

The <Directory> directive doesn't control "Directory type URLs". The DocumentRoot and the Alias directives controls where in the file system the content is found. Those directives can be used directly in the server config or inside a <VirtualHost>.

For what you are trying to achieve you simply need two <VirtualHost>s with different DocumentRoots, e.g.

Listen 80
<VirtualHost *:80>
    DocumentRoot "/var/www"
    ServerName localhost
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "/var/www/examplecom"
    ServerName example.com
</VirtualHost>

Now, you have both http://localhost/examplecom/ and http://example.com/ for the same website. If the sites aren't all under the same file system directory, you'd need to use Aliases, e.g.

Listen 80
<VirtualHost *:80>
    DocumentRoot "/var/www"
    ServerName localhost

    Alias "/examplecom" "/home/user/sites/example.com"
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "/home/user/sites/example.com"
    ServerName example.com
</VirtualHost>
Esa Jokinen
  • 46,944
  • 3
  • 83
  • 129
  • Looks good. Nowhere have I seen that "localhost" example. And do I put options like "Options Indexes" and "AllowOverride All" inside a block inside the ? – David Spector Aug 29 '18 at 13:49
  • Wow! It works! I've been struggling with this issue off and on for months, so thank you! I wish the Apache documentation could include this valuable information in a way that all of us can understand. – David Spector Aug 29 '18 at 14:08
  • Apache has a very good and comprehensive documentation of everything. It requires the patience to read and the ability to understand it. There are really clear examples, but not of every possible variation, as Apache HTTPd can do almost anything. – Esa Jokinen Aug 29 '18 at 14:53