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

我已经激活

LoadModule rewrite_module modules/mod_rewrite.s

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

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

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


当前回答

尝试设置:AllowOverride All。


第二个最常见的问题是没有启用mod重写:a2enmod重写,然后重新启动apache。

其他回答

如果没有以上工作尝试编辑/etc/apache2/sites-enabled/000-default

几乎在顶部你会发现

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

将AllowOverride None更改为AllowOverride All

这对我很有效

使用以下命令

sudo a2enmod rewrite

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

sudo service apache2 restart

为了使用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中的应用程序文件将无法访问。

对我有用的(在ubuntu中):

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

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

打开终端,输入a2enmod rewrite,它将为Apache启用mod_rewrite模块。

然后进入/etc/apache2/sites-available并编辑默认文件。(为此,您必须对该文件和站点可用文件夹具有可写权限。)

用现有的第4至14行替换下面的内容

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

现在通过/etc/init.重新启动apacheD /apache2 restart或service apache2 restart

再次进行干净的URL测试,这一次它将通过。