Search code examples
apache.htaccessvirtualhost

apache virtualhost site using .htaccess not working


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 [email protected]
    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.


Solution

  • 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.