正如您所指出的,问题在于。htaccess重写规则,在典型的wp htaccess文件中,该规则将物理文件和目录排除在索引处理之外。php并直接获得服务。我认为解决办法在于与合作。htaccess添加了一个优先规则,让带有查询字符串的文件由php处理,而不是直接提供,并释放查询字符串。我不认为这样的规则(仅限于特定情况)可能会使服务器过载,您可以轻松地监视它并决定是否使用它:在您的中尝试这个。htaccess:
#first condition if the URL contains the path to uploads directories
RewriteCond %{REQUEST_URI} /wp-content/uploads/ [NC]
#second condition if specific keys are present in the query string (attr1 OR attr2)
RewriteCond %{QUERY_STRING} attr1= [OR]
RewriteCond %{QUERY_STRING} attr2=
#third condition we need to avoid infinite loop and errror 500,
#so we check that a specific key (added in our final rewrite rule below) is not present
RewriteCond %{QUERY_STRING} !loop=no
#the final rule if all above is matched will redirect to index.php and add our \'loop=no\' key/value pair to avoid the loop
RewriteRule ^ /index.php?loop=no [L,QSA]
此时,如果键入url
/wp-content/uploads/2012/10/img.png?attr1=foo&attr2=bar
您将得到404未找到。在插件函数中,然后:
add_action(\'init\',\'check_attributes\');
function check_attributes(){
if(!empty($_GET[\'loop\']) && $_GET[\'loop\'] ==\'no\'){ //you may add more clause here if needed
$var1=$_GET[\'attr1\'];
$var2=$_GET[\'attr2\'];
// perform your logic with the variables passed to the url
// maybe redirect to the physical file without query vars?
wp_redirect(strtok($_SERVER["REQUEST_URI"], \'?\'));
exit;
}
}
希望能有所帮助