.htaccess重写改变REQUEST_URI

时间:2011-12-17 07:18:21

标签: .htaccess mod-rewrite

我有这样的链接:

http://domain/gotothispage

需要像这样处理:

http://domain/index.php?req=gotothispage

我使用以下规则设置.htaccess文件:

RewriteEngine on
RewriteRule ^(.*) /index.php?req=$1 

我发现重写工作正常,但是它会在脚本中产生错误。我没有正确地处理和显示页面,而是让环境变量在页面上吐出来,REQUEST_URI看起来像这样:

/gotothispage

我怀疑这会导致脚本可能出现以下格式的问题:

index.php?req=gotothispage

任何人都有关于如何编写我的规则来解决这个问题的建议吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

“但它正在使用脚本生成错误”=>我想到的问题是正在处理所有文件,包括JavaScript等静态文件。

以前可能需要一个小过滤器:

RewriteEngine On
# if not a static file...
RewriteCond %{REQUEST_URI} !(.*)\.(css|js|htc|pdf|jpg|jpeg|gif|png|ico)$ [NC]
# ...then rewrite to index, keep query string, and stop rewrite rules
RewriteRule ^(.*) /index.php?req=$1 [QSA,L]

现在有两个提示:

请尝试使用RewriteLog指令:它可以帮助您找出此类问题:

# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On

我最喜欢检查regexp的工具:

http://www.quanetic.com/Regex(别忘了选择ereg(POSIX)而不是preg(PCRE)!)