首页 > 解决方案 > 允许覆盖虚拟主机中的路径

问题描述

在 Windows 10 机器上映射到 Z: 驱动器的外部驱动器上有一些 PHP 文件。此代码位于httpd-vhosts.confApache 文件中:

<VirtualHost *:8080>    
    DocumentRoot "Z:/files/xampp/htdocs"
    <Directory "Z:/files/xampp/htdocs">
        Options Indexes
        Require all granted
    </Directory>
</VirtualHost>

手头的任务是允许.htaccess覆盖上述路径中的文件路径。在httpd.conf文件中启用了重写模块。该行如下所示:LoadModule rewrite_module modules/mod_rewrite.so. 我尝试将此代码添加到httpd.conf文件中:

<Directory "Z:/files/xampp/htdocs/miscellaneous/WebServiceExamples">
    AllowOverride All
</Directory>

进行这些更改后,Apache Web 服务器已停止并重新启动。中的代码.htaccess如下所示:

<IfModule mod_rewrite.c>

# turn rewrite engine on
Options +FollowSymlinks
RewriteEngine on

# map neat URL to internal URL
RewriteRule ^mobile/list/$   RestController.php?view=all [nc,qsa]
RewriteRule ^mobile/show/([0-9]+)/$   RestController.php?view=single&id=$1 [nc,qsa]

</IfModule>

但是,当在 Chrome 地址栏中输入应该由.htaccess文件中的重写规则处理的 URL 时,结果是找不到对象!错误 404。我在哪里误入歧途?

编辑:
我尝试的另一件事是httpd.conf恢复其原始状态,然后httpd-vhosts.conf进行如下修改:

<VirtualHost *:8080>    
    DocumentRoot "Z:/files/xampp/htdocs"
    <Directory "Z:/files/xampp/htdocs">
        Options Indexes
        Require all granted
    </Directory>
    <Directory "Z:/files/xampp/htdocs/miscellaneous/WebServiceExamples">
        AllowOverride All
    </Directory>
</VirtualHost>

停止并重新启动 Apache Web 服务器后,结果仍然和以前一样: 找不到对象!错误 404。

标签: apache.htaccess

解决方案


想通了。配置文件保持在原始帖子的编辑部分中描述的状态。此链接中的故障排除部分是确定配置文件设置正确的有用指南。

这是在 Chrome 的地址栏中输入的 URL:http://localhost:8080/miscellaneous/WebServiceExamples/SimpleRestfulWebService/mobile/list。请注意,没有尾部正斜杠。重写规则的.htaccess编写方式使得尾部正斜杠成为强制性的。当重写规则行被修改以使尾部斜杠可选时,它工作正常。以下是修改后的重写规则:

RewriteRule ^mobile/list/?$   RestController.php?view=all [nc,qsa]
RewriteRule ^mobile/show/([0-9]+)/?$   RestController.php?view=single&id=$1 [nc,qsa]

推荐阅读