【问题标题】:The browser shows 404 Not Found when I'm using a RewriteRule on the server当我在服务器上使用 RewriteRule 时,浏览器显示 404 Not Found
【发布时间】:2017-10-31 08:29:33
【问题描述】:

我必须将整个 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$

我错过了什么?

【问题讨论】:

  • janos@example:/var/www/example.hej.hu$ php -i|grep rewrite url_rewriter.tags => a=href,area=href,frame=src,input=src,form=fakeentry => a=href,area=href,frame= src,input=src,form=fakeentry

标签: apache .htaccess ubuntu mod-rewrite


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2018-04-22
    • 1970-01-01
    • 2019-01-13
    • 1970-01-01
    • 2016-06-30
    • 2019-12-09
    • 1970-01-01
    • 2016-09-02
    • 2016-07-30
    相关资源
    最近更新 更多