在 htaccess 中标记查询字符串并在 PHP 中检索它
Flagging a query string in htaccess and retrieving it in PHP
这是我的 .htaccess:
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} !^$
RewriteRule .* - [L]
RewriteCond %{QUERY_STRING} ^(.+)$ [OR]
RewriteCond %{REQUEST_URI} ^(.*)\?(.+)$
RewriteRule .* - [ENV=HASQSTRING:1]
# miscellaneous rewriterules to redirect pages.
# No environment variables changed here
这是我的 PHP 代码:
<?php
echo "QS=".$_ENV['HASQSTRING'];
?>
我什至试过这个 PHP 代码:
<?php
echo "QS=".$_SERVER['HASQSTRING'];
?>
我每次都收到这个结果:
Notice: Undefined index: HASQSTRING in (php filename) on line 2
我正在测试的 URL 总是有一个查询字符串,例如:
http://localhost.local/?a=1
http://localhost.local/?a=1&b=2
http://localhost.local/help/?a=1&b=2
如何修复我的 .htaccess,以便当原始 URL 中存在查询字符串时(不在 mod_rewrite 生成的内部重定向 url 中)的值HASQSTRING 等于 1?
我试过检查 REDIRECT_STATUS 的值,但它总是想要 return 200 的值。
你可以这样使用它:
RewriteCond %{ENV:REDIRECT_STATUS} .
RewriteRule ^ - [L]
RewriteCond %{QUERY_STRING} ^(.+)$
RewriteRule ^ - [E=HASQSTRING:%1]
然后在PHP代码中使用
<?php
echo "QS=" . $_SERVER['HASQSTRING'];
?>
这是我的 .htaccess:
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} !^$
RewriteRule .* - [L]
RewriteCond %{QUERY_STRING} ^(.+)$ [OR]
RewriteCond %{REQUEST_URI} ^(.*)\?(.+)$
RewriteRule .* - [ENV=HASQSTRING:1]
# miscellaneous rewriterules to redirect pages.
# No environment variables changed here
这是我的 PHP 代码:
<?php
echo "QS=".$_ENV['HASQSTRING'];
?>
我什至试过这个 PHP 代码:
<?php
echo "QS=".$_SERVER['HASQSTRING'];
?>
我每次都收到这个结果:
Notice: Undefined index: HASQSTRING in (php filename) on line 2
我正在测试的 URL 总是有一个查询字符串,例如:
http://localhost.local/?a=1
http://localhost.local/?a=1&b=2
http://localhost.local/help/?a=1&b=2
如何修复我的 .htaccess,以便当原始 URL 中存在查询字符串时(不在 mod_rewrite 生成的内部重定向 url 中)的值HASQSTRING 等于 1?
我试过检查 REDIRECT_STATUS 的值,但它总是想要 return 200 的值。
你可以这样使用它:
RewriteCond %{ENV:REDIRECT_STATUS} .
RewriteRule ^ - [L]
RewriteCond %{QUERY_STRING} ^(.+)$
RewriteRule ^ - [E=HASQSTRING:%1]
然后在PHP代码中使用
<?php
echo "QS=" . $_SERVER['HASQSTRING'];
?>