当 root 完美解析时,如何让 Apache 2.4 localhost 子目录解析?

Rya*_*yan 5 apache-http-server localhost osx-yosemite macos

根据这里的其他答案,我能够让 Apache 加载本地主机页面:

2.2 配置:

Order allow,deny
Allow from all
Run Code Online (Sandbox Code Playgroud)

2.4 配置:

Require all granted
Run Code Online (Sandbox Code Playgroud)

这适用于本地主机页面mylocalsite.local(返回完整页面,包括嵌套目录中的所需资产,如 css 和图像)。

但是当我尝试访问子目录时,mylocalsite.local/subdir它会返回Not Found - The requested URL /subdir was not found on this server.

请参阅下文了解我的虚拟主机的配置方式。显然,mylocalsite.local被设置为127.0.0.1/etc/hosts让我这么远。

<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>
Run Code Online (Sandbox Code Playgroud)

你能明白为什么这不能正确解决吗?

其他调试:

> 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
Run Code Online (Sandbox Code Playgroud)

在 Mac OS X Mavericks 10.9 中一切正常。

小智 3

我在这里找到了一个可行的解决方案http://mallinson.ca/osx-web-development/

请注意 Yosemite 的部件!

确保 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
Run Code Online (Sandbox Code Playgroud)