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

我已经激活

LoadModule rewrite_module modules/mod_rewrite.s

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

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

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


当前回答

对我有用的(在ubuntu中):

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

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

其他回答

显然有不止一种方法可以做到这一点,但我建议使用更标准的方法:

ErrorDocument 404 /index.php?page=404

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

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

现在重新启动apache。

service apache2 reload

希望它有用。

旧线程中,不要设置AllowOverride为all而是使用你想使用的特定mod,

AllowOverride mod_rewrite mod_mime

这一行应该取消注释

LoadModule rewrite_module modules/mod_rewrite.so

参考资料

http://www.eschrade.com/page/why-you-should-not-use-htaccess-allowoverride-all-in-production/ https://httpd.apache.org/docs/2.4/misc/security_tips.html https://httpd.apache.org/docs/2.4/rewrite/avoid.html

打开终端,输入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测试,这一次它将通过。

在Ubuntu中:

Run:

a2enmod rewrite

然后:

service apache2 restart

Mod_rewrite现在将被启用!