带有mod_rewrite的Apache 2.4.10返回'404 Not Found'

时间:2016-01-09 18:18:10

标签: apache .htaccess mod-rewrite

我在Apache 2.4.10(Debian)上本地开发Web服务 我想将网址用作以下内容:http://localhost/ws/rest/users
其中/ ws / rest是物理目录,用户是参数。

我写了以下/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”错误。

问题出在哪里?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

我解决了添加到/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

相关问题