使用mod_rewrite和重定向设置服务器变量


Set a server variable with mod_rewrite, as well as redirect

我有一个.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 对于我的情况来说不是有效的解决方案。

编辑:错误日志输出以供参考:

[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.
如果您希望

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"]