1

是否可以将我的网站查询字符串参数限制为我分配的那些。这样做时,我可以将任何在我的批准列表中找不到的带有查询字符串参数的 URL 重定向到我的 404 页面吗?

例如,我希望只允许 '?s=' 和 '?p=' 作为查询字符串参数,因此如果www.mysite.com/?x=whatever访问该站点,该站点将重定向该用户我的 404 页面 - 如果www.mysite.com/?s=whatever那时我的站点将显示适当的内容。

4

4 回答 4

2

如果你想用 .htaccess 来做,你可以做这样的事情:

RewriteCond %{REQUEST_URI} !(s=(.*)|404.html)
RewriteRule .* 404.html [R=404,L]

此外,您必须为 ?s= 动态生成页面,因此请确保为 index.php(或您正在使用的脚本)设置例外:

RewriteCond %{REQUEST_URI} !(^s=(.*)|404.html|index.php)
RewriteRule .* 404.html [R=404,L]

没有测试过,但这应该可以。

如果你想用 PHP 来做,那么只需检查 $_GET 变量,如果没有 ?s= 则重定向或显示 404 页面:

if (!(isset($_GET['s'])) {
    header('HTTP/1.0 404 Not Found');
    header('Expires: Thu, 19 Nov 1981 08:52:00 GMT');
    header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0, private');
    readfile('404.html');
    exit;
}

你明白了。

于 2011-10-21T14:05:28.830 回答
1

只需检查$_GET并查找是否有不允许的参数,然后重定向到您的 404 页面。

于 2011-10-21T13:55:12.347 回答
1

在 Apache 上,您可以使用 mod_rewrite... 类似以下内容:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^x=(allowed_values_of_x)$
RewriteRule ^path/in/uri$ /redirect/to/file?withquery=%1 [L]
RewriteCond %{QUERY_STRING} ^x=(.*)$
RewriteRule ^path/in/uri$ /redirect/to/404?withquery=%1 [R=404,L]

如果 的值x是有效的,它将重定向到具有有效x参数的文件,否则它应该重定向到具有无效x参数的 404 处理程序(因此,如果您愿意,可以对它做一些花哨的事情)。

查看 Apache mod_rewrite 条件: http ://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritecond

于 2011-10-21T14:01:26.667 回答
1

创建一个允许的查询字符串参数列表,如下所示:

$allowed_parameters = array( 's', 'q' );

如果 $_GET 数组包含允许的键之外的任何键,则重定向用户:

foreach ( $_GET as $key => value ) {
    if ( ! in_array( $key, $allowed_parameters ) ) {
        header( "Location: http://www.mysite.com/error404.html" );
        exit;
   }
}

用于exit立即停止处理。没有它,重定向将在处理完所有剩余的数组键后发生。

于 2011-10-21T14:02:10.913 回答