当我在服务器上使用RewriteRule时,浏览器显示404 Not Found

时间:2017-10-31 08:29:34

标签: apache .htaccess ubuntu mod-rewrite

我不得不重新安装整个Ubuntu 14到16.04.3才能将PHP升级到7.0。现在机器上有Ubuntu 16,但我的网站坏了。它使用的是Apache和mod_rewrite。

/etc/apache2/sites-available/000-default.conf

...
<VirtualHost *:80>
        ServerName api.example.com
        ServerAdmin janos@example.com
        DocumentRoot /var/www/api.example.com
        <Directory "/var/www/api.example.com">
                Options -Indexes
        </Directory>
        ErrorLog ${APACHE_LOG_DIR}/api.example.com.error.log
        CustomLog ${APACHE_LOG_DIR}/api.example.com.access.log combined
</VirtualHost>
...

在服务器上:

janos@example:/var/www/api.example.com$ ls -l .htaccess dir
-rw-rw-r-- 1 janos janos   65 okt   31 09:10 .htaccess

dir:
total 4
-rw-rw-r-- 1 janos janos 6 okt   31 09:09 hello.txt
janos@example:/var/www/api.example.com$ cat .htaccess 
RewriteEngine On
RewriteRule    "^test.txt$"     "dir/hello.txt"
janos@example:/var/www/api.example.com$ sudo service apache2 restart
janos@example:/var/www/api.example.com$ sudo a2enmod rewrite 
Module rewrite already enabled

在我的客户端:

mymachine:Downloads janos$ curl -Is  http://api.example.com/test.txt|grep HTTP
HTTP/1.1 404 Not Found
mymachine:Downloads janos$ curl -Is  http://api.example.com/dir|grep HTTP
HTTP/1.1 301 Moved Permanently
mymachine:Downloads janos$ curl -Is  http://api.example.com/dir/|grep HTTP
HTTP/1.1 403 Forbidden
mymachine:Downloads janos$ curl -Is  http://api.example.com/dir/hello.txt|grep HTTP
HTTP/1.1 200 OK
mymachine:Downloads janos$

我错过了什么?

1 个答案:

答案 0 :(得分:0)

我的错误是:我不允许用户定义的htaccess文件更改mod_rewrite和其他规则。正确的配置部分是:

    <Directory "/var/www/api.example.com">
            Options -Indexes
    # it's working now with this following 1 line added:
            AllowOverride all
    </Directory>