【问题标题】:apache mod_rewrite not working with .htaccess fileapache mod_rewrite 不适用于 .htaccess 文件
【发布时间】:2013-07-25 23:36:25
【问题描述】:

好的,我在使用 aws 或其他方面遇到了一些问题,以至于我似乎无法让 mod_rewrite 工作。

仅出于测试目的,我做了以下操作:

1 使用 aws 控制台从向导部署新的 ami 64 位实例

2 yum 安装了 apache

3 编辑 /etc/httpd/conf/httpd.conf: 这样

<Directory />
    Options FollowSymLinks
    AllowOverride None
</Directory>

看起来像

<Directory />
    Options FollowSymLinks
    AllowOverride All
</Directory>

4 确保

LoadModule rewrite_module modules/mod_rewrite.so

在文件中且未注释。

5 重启 apache:

 sudo /sbin/service httpd restart

6 创建了两个新文件:

/var/www/html/test1.html

包含:

this is test1!

/var/www/html/test2.html

包含:

this is test2!

7 创建文件:

/var/www/html/.htaccess

包含(总计):

RewriteEngine on
RewriteRule ^test1\.html$ test2.html [L]

8 去了:

http://[my aws server]/test1.html

我得到“这是 test1!”

我在这里做错了,但对于我的生活,我不知道是什么。非常感谢任何帮助...

编辑:我在 .htaccess 文件的开头添加了无意义的字符/数字,然后重新启动了 apache(不是 100% 确定需要,但是嘿……),什么也没发生。换句话说,我预计转到 url [aws server]/test1.html 会导致某种错误,但事实并非如此。我怀疑 apache 甚至没有读取 .htaccess 文件。

编辑:我在 httpd.conf 文件中添加了以下内容:

RewriteLog "/tmp/rewrite.log"
RewriteLogLevel 9

该文件是在我重新启动 apache 时创建的,但是当我转到我设置的任一页面时,那里没有任何内容。我在这里没有做一些非常非常基本的事情,但我不确定是什么......

【问题讨论】:

  • 请注意,我知道在 SO 上有很多像这样的问题。我一直在努力寻找解决我问题的方法。但如果有人能指出一个好的或完整的 mod_rewrite 故障排除问题,我将不胜感激!
  • 只是为了让解决方案更明显,因为它包含在我下面的评论中:httpd.conf 文件已经有一个 /var/www/html 部分,其中包含“AllowOverride None”。我没有改变它,而是试图添加第二个,但被忽略了。经验教训:首先检查以确保您没有复制已经存在的部分! :)

标签: linux .htaccess mod-rewrite amazon-web-services


【解决方案1】:

不确定这是否是你的问题的原因,但你不应该弄乱

<Directory />
    Options FollowSymLinks
    AllowOverride None
</Directory>

行,应该是这样的:

<Directory />
    Options FollowSymLinks
    AllowOverride None
    Deny from all
</Directory>

你应该add the directory of your document root as a different container:

<Directory /var/www/html/>
    Options FollowSymLinks
    AllowOverride All
    Allow from all
</Directory>

【讨论】:

  • 谢谢乔恩。我已经看到您概述的是“正确”的方式,我只是想提出最简单的测试。我将目录选项重新设置为默认值,并将您在下面概述的代码添加到我的 httpd.conf 文件中。由于默认情况下我的 docroot 是您指定的目录,因此我保存并重新启动了 apache。不幸的是,这似乎并没有解决问题:(
  • @snuggles 我能想到的唯一另一件事是检查AccessFileName 的配置,它应该设置为.htaccess,但如果是别的东西那么你的.htaccess 文件将完全忽略。还要确保 .htaccess 文件可以被 apache 进程读取。
  • 您引导我走向正确的方向:httpd.conf 文件已经有一个 /var/www/html 部分,其中包含“AllowOverride None”。坦率地说,我只是没想到要在 conf 文件中寻找一个已经存在的部分,我认为它只是默认的。哎呀。我设置了“AllowOverride All”,一切正常。谢谢乔恩!
【解决方案2】:

我花了一段时间才找到这个,但在某些安装中,Apache 会使用多个配置文件。

查看“/etc/apache2/sites-enabled/000-default”并检查 AllowOveride 是否设置为 All

【讨论】:

  • 这让我真的不应该......谢谢!
【解决方案3】:

试试看。这对我有用。 首先,您需要确保 .htaccess 文件放在正确的目录中。 为此,您转到启用站点的文件夹并检查启用了哪些 .conf 文件。

cd /etc/apache2/sites-enabled
ls

例如:000-default.conf

然后,转到站点可用文件夹以编辑该 .conf 文件。

cd ../sites-available
sudo gedit 000-default.conf

找到 DocumentRoot 并再次检查目录。 如果您将 .htaccess 文件放在 /var/www/html/.htaccess 中,则此行如下所示:

DocumentRoot /var/www/html/

第二,你需要修改&lt;Directory&gt;块看起来像这样。

    <Directory /var/www/html>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Order allow,deny
    allow from all
    # This directive allows us to have apache2's default start page
            # in /apache2-default/, but still have / go to the right place
            #RedirectMatch ^/$ /apache2-default/
    </Directory>

最后,保存文件并重启 apache

service apache2 restart

希望对您有所帮助!

【讨论】:

    猜你喜欢
    • 2011-07-04
    • 2010-12-20
    • 1970-01-01
    • 2014-05-26
    • 2013-03-19
    • 1970-01-01
    • 2016-10-15
    • 2018-02-17
    • 1970-01-01
    相关资源
    最近更新 更多