来自root的Mod_rewrite规则

时间:2012-04-16 13:58:59

标签: apache mod-rewrite

这个问题一直困扰着我一段时间。

我创建了一个小型网站引擎,我正在使用mod_rewrite告诉引擎哪些页面可以处理,SEO友好链接是一个奖励:)。

今天它的工作方式如下:

地址http://www.example.com/site/page 变为http://www.example.com/engine.php?address=page

但我想要的是: 地址http://www.example.com/page 变为http://www.example.com/engine.php?address=page

如果我为调用(/ site)创建一个psuedo目录,那么一切正常,但是当我尝试从根目录开始做同样的事情时,事情就开始发生了。

RewriteBase /
RewriteRule ^site/(.*) engine.php?%{QUERY_STRING}&address=$1

正常工作:/ site / about / contacts成为eninge.php?address = about / contacts

RewriteBase /
RewriteRule ^(.*)$ eninge.php?%{QUERY_STRING}&address=$1

不起作用,由于某种原因/ about / contacts变成了eninge.php?address = eninge.php

3 个答案:

答案 0 :(得分:0)

检查RewriteLog(已在2.4中更新,如果不使用2.2则检查当前文档):

RewriteLog "/usr/local/var/apache/logs/rewrite.log"
RewriteLogLevel 3 

这将显示mod_rewrite正在做什么,并允许您根据其输出调整配置。注意 - 它的增长非常快,不应该在生产环境中使用。

顺便说一下,你的帖子中有一些拼写错误 - 值得验证这些与你的配置不同。

答案 1 :(得分:0)

RewriteCond %{REQUEST_FILENAME} !^engine.php
RewriteRule (.*) engine.php?address=$1 [QSA,L]

试试这个。你所拥有的是导致重写循环,并按照你的预期首先进行engine.php?address=about/contacts,然后再次转向并重写为engine.php?address=engine.php。合理? [QSA,L]是一个Query String Append和Last标志,它将查询字符串添加到您的URL并告诉重写引擎停止查找重写。 RewriteCond %{REQUEST_FILENAME} !^engine.php是通过确保当前URL不以engine.php开头来检查您是否尚未指定引擎重写。如果您在.htaccess文件而不是.httpd配置文件中编写此文件,则必须执行此操作。

答案 2 :(得分:0)

(.*)意味着抓住任何东西。你有没有试过在你的全能之前排除文件和目录?因为没有它会导致无限递归。

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ eninge.php?%{QUERY_STRING}&address=$1 [L]

官方文档中提供了更多信息:http://httpd.apache.org/docs/current/mod/mod_rewrite.html

更新:您还应在规则末尾指定[L],告诉Apache在此处结束重写过程。

相关问题