【问题标题】:Set a server variable with mod_rewrite, as well as redirect使用 mod_rewrite 设置服务器变量,以及重定向
【发布时间】:2013-10-08 13:41:10
【问题描述】:

我有一个 .htaccess 文件,它可以执行多个重写命令并且运行良好。然而,在测试它时,我遗漏了一条关键信息,在 $_SERVER 变量中设置了 id(我在我的 php 脚本中使用了 $_SERVER)。

我有一个 URL example.com/f/847f34c76f64cad96effc8e9c3cea176,其中长字符串代表一个 id,f 代表处理请求的脚本。

我有以下重写,它处理脚本部分。

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteCond %{REQUEST_URI} !=/robots.txt
RewriteCond %{REQUEST_URI} ^/f/(.*)$ [NC]
RewriteRule ^ /scriptf.php [L]

就像我说的,这可以正确处理重定向。但是,我还需要将 id 放在环境变量中。我就是这么想的

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteCond %{REQUEST_URI} !=/robots.txt
RewriteCond %{REQUEST_URI} ^/f/(.*)$ [NC]
RewriteRule ^ - [E=id:$1]
RewriteRule ^ /scriptf.php [L]

但遗憾的是这不起作用。

另外,在查询中发送 id 不是我的情况的有效解决方案。

EDIT:错误日志输出供参考:

[Tue Oct 08 10:48:31 2013] [error] [client 192.168.77.53] Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.

【问题讨论】:

  • 看起来你在正确的轨道上,但你需要在你的 env 行中使用 %1 而不是 $1,因为你从 RewriteCond 匹配它。
  • 我确实将其更改为 %1,但仍然收到 500 错误
  • 查看您的错误日志。在那里你会发现 500 错误实际上是什么。
  • 啊,这是因为您的 RewriteConds 仅适用于第一个 RewriteRule。您可以将[E=id:%1] 放在指向scriptf.phpRewriteRule 中,因为[L,E=id:%1] 应该可以工作,我认为。
  • 这似乎可行,创建了一个名为 REDIRECT_id 的变量。有什么办法可以将其更改为只是 id?还是这就是它的工作方式?

标签: php apache .htaccess mod-rewrite


【解决方案1】:

如果您希望 id$_GET['id'] 中可用,请将您的代码更改为:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^f/(.+)$ /scriptf.php?id=$1 [L,QSA,NC]

或者,如果您希望 id$_SERVER 数组中可用,请将您的代码更改为:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^f/(.+)$ /scriptf.php [L,NC,E=ID:$1]

然后你可以在 PHP 中使用以下变量:

$_SERVER["REDIRECT_ID"]

【讨论】:

  • "另外,在查询中发送 id 对我来说不是一个有效的解决方案。"
  • @MichaelBerkowski:但 OP 没有写:I left out one critical piece of information, setting the id in the $_SERVER variables
  • 我还提供了一个替代规则,使id 可用作 PATH_INFO(以防更适合 OP)
  • 我需要类似 $_SERVER["id"] = 847f34c76f64cad96effc8e9c3cea176
  • @ScottJoudry:检查编辑后的答案。该 ID 将以 $_SERVER["REDIRECT_ID"] 的形式提供
猜你喜欢
  • 2011-03-03
  • 1970-01-01
  • 1970-01-01
  • 2011-04-22
  • 2011-09-04
  • 1970-01-01
  • 1970-01-01
  • 2015-06-14
  • 1970-01-01
相关资源
最近更新 更多