Macos – How to get Apache 2.4 localhost subdirectories to resolve when root resolves perfectly

apache-http-serverlocalhostmacososx-yosemite

Based on other answers here I was able to get Apache to load localhost pages:

2.2 configuration:

Order allow,deny
Allow from all

2.4 configuration:

Require all granted

This works great for localhost pages like mylocalsite.local (returns full page, including required assets in nested directories like css and images).

But when I try to visit subdirectories like mylocalsite.local/subdir it returns Not Found - The requested URL /subdir was not found on this server.

See below for how my virtual hosts is configured. Obviously mylocalsite.local is set to 127.0.0.1 in /etc/hosts to get me this far.

<VirtualHost *:80>
    ServerName mylocalsite.local
    ServerAlias www.mylocalsite.local mylocalsite_alt.local

    <Directory /Users/my_username/Sites/mylocalsite/html>
        AllowOverride none
        Options all
        Require all granted
        Deny from none

        <IfModule mod_rewrite.c>
            Options +FollowSymLinks -MultiViews
            RewriteEngine On

            RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
            RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

            # Redirect all /img/abc to /img/index.php/abc
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteRule ^img/(.*) img/index.php/$1 [L]

            # redirect all directories to root using PATH variables
            # Ex. /abc/def/ redirects to /index.php/abc/def/
            RewriteCond %{REQUEST_URI} !=/server-status
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteRule (.*) index.php/$1 [L]
        </IfModule>
    </Directory>

    DocumentRoot /Users/my_username/Sites/mylocalsite/html
</VirtualHost>

Can you see why this is not resolving correctly?

Other debugging:

> sudo apachectl configtest
Syntax OK

> httpd -S
VirtualHost configuration:
*:80                   is a NameVirtualHost
     default server sandbox (/private/etc/apache2/vhosts/virtual_hosts.conf:8)
     port 80 namevhost sandbox (/private/etc/apache2/vhosts/virtual_hosts.conf:8)
             alias sandbox
     port 80 namevhost mylocalsite.local (/private/etc/apache2/vhosts/virtual_hosts.conf:20)
             alias www.mylocalsite.local
             alias mylocalsite_alt.local
ServerRoot: "/usr"
Main DocumentRoot: "/Users/my_username/Sites"
Main ErrorLog: "/private/var/log/apache2/error_log"
Mutex default: dir="/private/var/run/" mechanism=default 
Mutex mpm-accept: using_defaults
Mutex proxy-balancer-shm: using_defaults
Mutex proxy: using_defaults
PidFile: "/private/var/run/httpd.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="_www" id=70 not_used
Group: name="_www" id=70 not_used

Everything was working flawlessly in Mac OS X Mavericks 10.9.

Best Answer

  • I found a working solution here http://mallinson.ca/osx-web-development/

    Please notice the parts for Yosemite!

    Make sure the following are uncommented in httpd.conf:

    LoadModule vhost_alias_module libexec/apache2/mod_vhost_alias.so
    LoadModule php5_module libexec/apache2/libphp5.so
    LoadModule alias_module libexec/apache2/mod_alias.so
    LoadModule rewrite_module libexec/apache2/mod_rewrite.so
    Include /private/etc/apache2/extra/httpd-vhosts.conf
    
  • Related Question