【问题标题】:apache mod_rewrite is not working or not enabledapache mod_rewrite 不工作或未启用
【发布时间】:2011-12-10 14:43:16
【问题描述】:

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

我创建了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>

【问题讨论】:

    标签: apache mod-rewrite


    【解决方案1】:

    尝试设置:“AllowOverride All”。

    【讨论】:

    • 这个答案对我有用,如果我可以提出一个友好的建议,我认为最好将链接中的更多答案带入您的答案中。
    【解决方案2】:

    请尝试

    sudo a2enmod rewrite
    

    或使用正确的 apache 重启命令

    sudo /etc/init.d/apache2 restart 
    

    【讨论】:

    • 但我没有“a2enmod”命令。因为,我的 apahce 是 [yum install apache]
    • 您使用哪个操作系统? (CentOS、Ubuntu 等)
    • 我的操作系统是“AWS EC2 t1.micro”。抱歉,稍后忙。
    【解决方案3】:

    它正在工作。

    我的解决办法是:

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

    2.写了一些规则,比如:

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

    3.重新启动您的 Apache 服务器。

    4.自己再试一次。

    【讨论】:

    • 很高兴您找到了解决方案,但不要用另一个问题来完成您的回答。
    • 请注意,如果您将这些指令放入 httpd.conf 而不是 .htaccess,则需要 Directory 标签。
    【解决方案4】:

    为了让 mod_rewrite 在 Apache 2.4 中为我工作,我必须在下面添加“要求所有授权”行。

    <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 指令并查看它是否有效。上面的示例可用于通过在路径中包含子文件夹来确保不会从缓存中提供站点,尽管文件位于服务器的根目录中。

    【讨论】:

      【解决方案5】:

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

      来自 AllowOverride 无 允许覆盖所有

      【讨论】:

        猜你喜欢
        • 2012-12-19
        • 1970-01-01
        • 2015-09-22
        • 1970-01-01
        • 2016-03-31
        • 2011-08-12
        • 2018-09-14
        • 1970-01-01
        • 2011-06-29
        相关资源
        最近更新 更多