我已经在我的Vista机器上安装了新的Apache 2.2,一切都很好,除了mod重写。

我已经激活

LoadModule rewrite_module modules/mod_rewrite.s

但我的重写规则都没用,即使是像这样简单的

RewriteRule not_found %{DOCUMENT_ROOT}/index.php?page=404

我使用的所有规则都在我的主机上工作,所以他们应该是好的,所以我的问题是,在apache配置中有任何隐藏的东西,可以阻止mod重写?


当前回答

当我第一次纠结于mod_rewrite规则忽略我的流量时,我(沮丧地)发现我把它们放在了错误的<VirtualHost>中,这意味着我的流量将忽略所有它们,无论它们写得多好。确保这不会发生在你身上:

#更改日志位置以适合您的系统。 RewriteLog /var/log/apache-rw.log RewriteLogLevel 2

如果您重新启动Apache,这些参数将被激活,因此您可以重新使用它们并密切监视mod_rewrite行为。一旦你的问题解决了,把RewriteLogLevel调低,庆祝一下。

在我100%的经验中,我发现RewriteLog帮助我发现了重写规则的问题。我怎么推荐都不为过。祝您在故障排除中好运!

此外,这个书签是你最好的朋友: http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritelog

其他回答

在Ubuntu中:

Run:

a2enmod rewrite

然后:

service apache2 restart

Mod_rewrite现在将被启用!

新的apache版本在某些方面有所改变。如果你的apache版本是2.4,那么你必须去/etc/apache2。将会有一个名为apache2.conf的文件。你必须编辑它(你应该有根权限)。像这样更改目录文本

<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

现在重新启动apache。

service apache2 reload

希望它有用。

使用以下命令

sudo a2enmod rewrite

并通过下面的命令重新启动apache

sudo service apache2 restart

对我有用的(在ubuntu中):

sudo su
cd /etc/apache2/mods-enabled
ln ../mods-available/rewrite.load rewrite.load

另外,正如前面提到的,确保在/etc/apache2/sites-available/default的相关部分设置AllowOverride all

为了使用mod_rewrite,你可以在终端输入以下命令:

sudo a2enmod rewrite

之后重新启动apache2

sudo /etc/init.d/apache2 restart

or

sudo service apache2 restart

或按新的统一系统控制方式

sudo systemctl restart apache2

然后,如果您愿意,您可以使用下面的.htaccess文件。

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
</IfModule>

上面的.htaccess文件(如果放在DocumentRoot中)将把所有流量重定向到DocumentRoot中的index.php文件,除非该文件存在。

假设你有如下的目录结构,httpdocs是DocumentRoot

httpdocs/
    .htaccess
    index.php
    images/
        hello.png
    js/
        jquery.js
    css/
        style.css
includes/
    app/
        app.php

任何存在于httpdocs中的文件都将使用上面所示的.htaccess提供给请求者,然而,其他所有文件将被重定向到httpdocs/index.php。include /app中的应用程序文件将无法访问。