Apache mod_rewrite 在 .htaccess 中有效,但在 httpd.conf 中无效
我第一次尝试 mod_rewrite (我是网络新手,但正在努力学习)。我试图让 bob.html 重定向到 alice.html (阅读:URL 保持不变,页面内容为 alice.html )。这两个文件都位于 /var/www/ 中。我正在运行 Ubuntu 10.10 w/Apache 2.2.16。
以下是放置在 /var/www/ 目录中的 .htaccess 文件中的工作原理:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^bob.html$ alice.html
</IfModule>
它的行为符合预期,但当移动到 httpd.conf 文件时它不起作用(只是学习性能等方面的最佳实践)。我在某处读到,在 httpd.conf 文件中使用 mod_rewrite 时,需要前导斜杠,因此我的 httpd.conf 文件如下所示:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/bob.html$ /alice.html
</IfModule>
我还读到 /etc /apache2/sites-enabled/000-default 需要将 AllowOverride 设置为 All。
<Directory /var/www>
...
AllowOverride All
...
</Directory>
我认为这只适用于.htaccess,但我不确定,所以我只是将其保留。
使用所有这些配置设置,重定向不起作用。我已经测试过通过插入错误代码来确保文件本身被读取(有人建议不再使用 httpd.conf 并使用 apache2.conf ) 。我已经查看了其他几个问题/答案,但我仍然无法弄清楚。
编辑:应该注意的是,我在每次更改 httpd.conf 后使用 /etc/init.d/apache2 restart 重新启动Apache 并(希望)重新加载配置。
I'm experimenting with mod_rewrite for the first time (I'm a web newbie, but trying to learn). I'm trying to get bob.html to redirect to alice.html (read: URL stays the same, page content is alice.html). Both files are in /var/www/. I'm running Ubuntu 10.10 w/Apache 2.2.16.
Here's what works in the .htaccess file placed in the /var/www/ directory:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^bob.html$ alice.html
</IfModule>
This behaves as expected, but it does not work when moved to the httpd.conf file (just learning best practices w/performance and such). I read somewhere that when using mod_rewrite in httpd.conf file, leading slashes are required, so my httpd.conf file looks like:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/bob.html$ /alice.html
</IfModule>
I also read that /etc/apache2/sites-enabled/000-default needs to have AllowOverride set to All.
<Directory /var/www>
...
AllowOverride All
...
</Directory>
I think that's just for use with .htaccess but I wasn't sure so I just left it in.
With all of these configuration settings, the redirection does not work. I've tested to make sure the file itself is being read (someone suggested httpd.conf isn't used anymore and apache2.conf is used instead) by inserting erroneous code. I've taken a look at a couple other questions/answers but I still cant figure it out.
Edit: It should be noted that I'm using /etc/init.d/apache2 restart after each change to httpd.conf to restart Apache and (hopefully) reload the configuration.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将重写规则包装在 httpd.conf 文件中的 中。标签。这似乎改变了行为,使其更类似于 .htaccess 文件。
Try wrapping the rewriterules in your httpd.conf file in <Location /></Location> tags. The seems to alter the behavior making it more similar to the .htaccess file.