0

I am running brew/apache on on my local machine (mac-os monterrey).

I want to configure virtual hosts.

The lookup for one of the virtual host (panierssaison.local) does not work, ending in a server not found error after a few seconds delay.

Apache is set up to listen on port 8080, virtual hosts are allowed in httpd.conf

Here is the configuration for the problematic host in httpd-vhosts.conf:

<VirtualHost *:8080>
    ServerAdmin eniac314@panierssaison.local
    DocumentRoot "/Users/eniac314/Sites/paniersSaison"
    ServerName panierssaison.local
    ServerAlias www.panierssaison.local
    ErrorLog "/opt/homebrew/var/log/httpd/panierssaison.local-error_log"
    CustomLog "/opt/homebrew/var/log/httpd/panierssaison.local-access_log" common

    <Directory "/Users/eniac314/Sites/paniersSaison">
        Options Indexes FollowSymLinks Multiviews
        MultiviewsMatch Any
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost> 

I added the virtual hosts in hosts.conf like so

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1   localhost panierssaison.local laterreduchat.local murol.local
255.255.255.255 broadcasthost
::1             localhost

The site not loading uses an .htaccess file inside /Users/eniac314/Sites/paniersSaison, removing it allows the site to load.

.htaccess relevant rules:

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteBase /

[..]

When running httpd -S all the virtual hosts are detected correctly.

fayong lin
  • 214
  • 2
  • 15
  • _"The problem might be related to the .htaccess file inside /Users/eniac314/Sites/paniersSaison"_ - should be easy enough to replace that "might" with a yes or no then, if you remove that .htaccess file. Still same error, or not? – CBroe Apr 22 '22 at 10:26
  • Removing the .htaccess allows the site to load, I did make that test before but it did not work, probably because of caching. I edited accordingly. – fayong lin Apr 22 '22 at 12:00
  • So I am guessing it is not the first request that fails with this error, but you get redirected somewhere, and then when the browser tries to load that new URL, it contains an invalid server name ... Have you checked what redirect response you actually get, and what request your browser tries to make next, in the network panel? – CBroe Apr 22 '22 at 12:01
  • With the browser cache interfering it's hard to see what's going on. It seems requesting panierssaison.local/index.html gets a 301, redirecting to www.panierssaison.local.html, which in turns gets nothing. – fayong lin Apr 22 '22 at 12:21
  • I don't see any explicit redirects to anything ending in `.html`, so I am guessing that it is probably one of those `RewriteRule . %{REQUEST_URI}?locale=...[R=301,L,NE,QSA]` lines where it goes wrong. As always, when in doubt - enable rewrite logging, then you can check on what is actually going on in detail. – CBroe Apr 22 '22 at 12:35

1 Answers1

0

So it seems adding the server aliases with www prefixes in hosts.conf fixed the problem.

Here is my hosts.conf now:

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1   localhost panierssaison.local laterreduchat.local murol.local www.panierssaison.local www.laterreduchat.local www.murol.local
255.255.255.255 broadcasthost
::1             localhost

I think this rule in .htaccess

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

was appending the www prefix to the url, preventing the dns match.

fayong lin
  • 214
  • 2
  • 15
  • I'm glad this worked for you, but correcting your omission isn't likely to help anybody with similar problems in the future. – Stephen Ostermiller Apr 23 '22 at 09:42
  • Is there any thing I could add to make my answer more useful? – fayong lin Apr 23 '22 at 11:19
  • You could focus your question. Right now it has a huge dump of code and configuration and doesn't contain any of the debugging information you would need to figure out that your answer is actually the problem. – Stephen Ostermiller Apr 23 '22 at 12:57
  • I tried rewriting the question more clearly. I looked into apache's log but it's actually the delay before the the error that got me thinking the problem might be with the dns lookup rather than with apache configuration. I did not mark my answer as accepted since I do not know if one is supposed to add server name aliases in hosts.conf like I did. – fayong lin Apr 23 '22 at 13:23
  • I suspect that the RewriteRule is missing a slash (`/`) and should be: `RewriteRule ^ http%1://www.%{HTTP_HOST}/%{REQUEST_URI} [R=301,L]`. – Andrew Vickers Apr 28 '22 at 19:26
  • Changing the rule adds an extra unneeded slash. http://panierssaison.local:8080/accueil redirects to http://www.panierssaison.local:8080//accueil – fayong lin Apr 28 '22 at 22:13