是否有一种方法使用php或htaccess来防止访问url的某个部分


Is there a way using php or htaccess to prevent access to a certain part of an url

例如,用户不能访问http://example.com/here,但可以访问添加了more的任何内容。例如http://example.com/her?action=blah blah。

这样做的目的是防止通过直接访问页面来访问文件,但是允许文件在添加了更多操作/链接的情况下使用。

我只想感谢大家的快速反应。根据我得到的答案,我使用php,它工作。

我使用了以下语句:

function access_granted(){
 global $pagenow;
 if(!isset($_GET['action']) && 'wp-login.php' == $pagenow ) {
  wp_redirect('http://example.com/');
  exit();
 }
}
但是我有一个问题。我把wp-login.php的url改为http://example.com/here,而不是http://example.com/wp-login.php。

我的问题是,现在,函数access_granting不再工作了。我猜是$pagenow

有什么建议,比如如何检查url/这里而不是wp-login.php?

如果你想使用。htaccess方式,就这样做。通过httpd.conf启用mod_rewrite和.htaccess,然后将此代码放在DOCUMENT_ROOT目录下的.htaccess中:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^here/?$ - [NC,L,F]

这将抛出/here/here/的禁止错误,但将允许/here?bar=foo类型的uri

是的,有。你不需要处理htaccess,你可以用php。首先,您需要检查是否设置了必需的选项。例如,如果您有一个名称为actionGET属性,您可以检查它是否使用isset函数设置。您还需要指定一个自定义HTTP 403页面,并将用户重定向到该页面。下面是一个示例,如果action未定义,则显示禁止错误:

<?php
if (!isset($_GET['action'])) {
   header('HTTP/1.0 403 Forbidden',403);
   header('Location: 403.html');
   exit;
}
?>

你可能也会发现下面的链接很有用:如何在PHP中显示Apache's默认的404页面

example.com/here是由PHP脚本处理?如果是这样,在脚本开始时进行输入验证,如果不符合要求,则给出错误页面或重定向(http://php.net/manual/en/function.http-redirect.php)到另一个页面。

如果/here是由PHP处理的,那么如果你想让它在URL中添加任何内容时工作,你可以使用这个:

 <?php
  if(!$_GET){
         die('You are not allowed to access this URL.');
   }
   else {
     ... PHP code that will execute in case the user is allowed to access the URL(you can also use HTML by just having ?> here instead and add <?php } ?> at the end of the document)  ...
  }
 ?>

希望有帮助。

当然,您可以使用PHP。在here.php中,您需要在代码的顶部添加一行(在您的示例中),如下所示:

if(!isset($_GET['action']){
     header('location:/notallowed.php');
     exit;
}

其中notallowed.php是当他们没有完整链接时你想显示的任何内容