Apache的mod_rewrite不工作或未启用

我已经在Apache上安装了rewrite_module并修改了php.ini

我创build了rewrite.php.htaccess文件,但它不工作。

我的文件系统文件夹如:

 /var/www/html /var/www/html/test /var/www/html/test/.htaccess /var/www/html/test/rewrite.php 

/var/www/html/.htaccess的内容是:

 $ cat /var/www/html/test/.htaccess RewriteEngine On RewriteRule ^link([^/]*).html$ rewrite.php?link=$1 [L] 

/var/www/html/rewrite.php

 $ cat /var/www/html/test/rewrite.php <html> <h2 align=center> <?php // mod_rewrite Test Page // Copyright 2006 Webune.com if($_GET['link']==1){echo"You are not using mod_rewrite";} elseif($_GET['link']==2){echo"Congratulations!! You are using Apache mod_rewrite";} else{echo"Linux Apache mod_rewrte Test Tutorial";} ?> </h2> <hr> <head> <title>How To Test mod_rewrite in Apache Linux Server</title> </head> <body> <p align="center">by <a href="http://www.webune.com">Webune</a></p> <p><a href="rewrite.php?link=1">LINK1</a> = rewrite.php?link=1</p> <p><a href="link2.html">LINK2</a> = link2.html</p> <p>How this works: both links are for this same page, except they both are different. link one is without the mod_rewrite and link2 is using mod_rewrite. Link1 show the php file, with with mod_rewrite we are mascarading the php file into a html file. you can use whatever type of extension you want, you can change it to .htm or .shtml etc... all you have to do is to make sure you also chang it in the .htaccess file</p> <p>&lt;&lt; <a href="http://www.webune.com/forums/viewtopic-p-62.html">Go back to webune forums.</a></p> </body> </html> 

已安装mod_rewrite.so。

 $ ls -l mod_rewrite.so -rwxr-xr-x 1 root root 59256 Sep 20 23:34 mod_rewrite.so 

加载了rewrite_module。

 $ cat /etc/httpd/conf/httpd.conf | grep mod_rewrite.so LoadModule rewrite_module modules/mod_rewrite.so 

AllowOverride设置

 $ cat /etc/httpd/conf/httpd.conf <Directory "/var/www/html"> Options Indexes FollowSymLinks AllowOverride None Order allow,deny Allow from all </Directory> 

我在一篇文章中写到: http : //www.jarrodoberto.com/articles/2011/11/enabling-mod-rewrite-on-ubuntu

尝试设置:“AllowOverride All”。

它正在工作。

我的解决scheme是:

1.在/etc/httpd/conf.d/test.conf中创build一个test.conf

2.写一些规则,如:

 <Directory "/var/www/html/test"> RewriteEngine On RewriteRule ^link([^/]*).html$ rewrite.php?link=$1 [L] </Directory> 

重新启动你的Apache服务器。

再次尝试。

请尝试

 sudo a2enmod rewrite 

或者使用正确的apache重新启动命令

 sudo /etc/init.d/apache2 restart 

为了让mod_rewrite在Apache 2.4中为我工作,我不得不在下面添加“Require all granted”行。

 <Directory /var/www> # Required if running apache > 2.4 Require all granted RewriteEngine on RewriteRule ^cachebust-([a-z0-9]+)\/(.*) /$2 [L] </Directory> 

据推测Apache 2.2也存在类似的需求,如果你使用的是:

 <Directory /var/www> # Required if running apache 2.2 Order allow,deny Allow from all RewriteEngine on RewriteRule ^cachebust-([a-z0-9]+)\/(.*) /$2 [L] </Directory> 

请注意, ErrorDocument 404指令有时也可以重写这些内容,所以如果不工作,请尝试注释掉您的ErrorDocument指令并查看是否有效。 上面的例子可以用来确保站点不是通过在path中包含一个子文件夹从caching中提供的,尽pipe这些文件驻留在服务器的根目录下。

在centOS7上,我更改了/etc/httpd/conf/httpd.conf文件

从AllowOverride无到AllowOverride全部