【问题标题】:Apache 2.4.10 with mod_rewrite returns '404 Not Found'带有 mod_rewrite 的 Apache 2.4.10 返回“404 Not Found”
【发布时间】:2016-01-09 18:18:09
【问题描述】:

我在 Apache 2.4.10 (Debian) 上本地开发 Web 服务。
我想像这样使用 URL:http://localhost/ws/rest/users
其中 /ws/rest 是物理目录,users 是一个参数。

我写了以下 /var/www/html/ws/rest/.htaccess 文件:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^(.*)$ ws/rest/api.php?rquest=$1 [QSA,NC,L]

RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ ws/rest/api.php [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^(.*)$ ws/rest/api.php [QSA,NC,L]   
</IfModule>

这个想法是将所有内部路径重定向到 api.php 文件,但是当我将浏览器指向上一个 URL 时,我总是收到“404 Not found”错误。

问题出在哪里?

【问题讨论】:

    标签: apache .htaccess mod-rewrite


    【解决方案1】:

    您缺少RewriteBase 指令。

    由于您的.htaccess 文件位于您的/ws/rest/ 目录中,并且此路径是您的网址的一部分,您可以使用/ws/rest/ 作为RewiteBase

    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /ws/rest/
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-s
        RewriteRule ^(.*)$ api.php?rquest=$1 [QSA,NC,L]
    
        RewriteCond %{REQUEST_FILENAME} -d
        RewriteRule ^(.*)$ api.php [QSA,NC,L]
        RewriteCond %{REQUEST_FILENAME} -s
        RewriteRule ^(.*)$ api.php [QSA,NC,L]   
    </IfModule>
    

    RewriteBase documentation

    【讨论】:

    • 抱歉,没有任何改变,还是 404。有没有办法知道重写是否处于活动状态?我在 /etc/apache2/mods-enabled/ 目录中有 rewrite.load ...
    • 我在 phpinfo() 和 apache_get_modules() 列表中检查了“加载的模块”:都报告了 mod_rewrite 已启用。我在 .htaccess 文件的顶部插入了一些垃圾字符,再次出现 404 错误。我应该收到其他错误吗????
    • 你应该。如果您将文件test.html 放在/var/www/html/test.html 下,您可以使用localhost/test.html 访问它吗?
    【解决方案2】:

    我解决了添加到/etc/apache2/sites-available/000-default.conf 文件的问题:

    <Directory /var/www/html>
                    Options Indexes FollowSymLinks MultiViews
                    AllowOverride All
                    Order allow,deny
                    allow from all
    </Directory>
    

    然后重启apache

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-16
      • 2018-12-18
      • 2021-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-01
      相关资源
      最近更新 更多