是否可以在某些网址上阻止 POST 方法,例如
www.mydomain.com/dontposthere
www.mydomain.com/something/againdontposthere
在 htaccess 中?
如果您有权访问 mod_rewrite,您可以检查
REQUEST_METHOD
环境变量并将 url 重写到另一个页面,该页面显示一条消息,内容类似于“您不允许发帖”
创建一个包含消息的页面:
/var/www/noPost.php
You are not allowed to post to this page
然后使用 .htaccess 规则,如下所示:
RewriteEngine On
RewriteCond %{REQUEST_METHOD} POST
RewriteCond %{REQUEST_URI} ^/dontposthere [OR]
RewriteCond %{REQUEST_URI} ^/something/againdontposthere
RewriteRule .* /noPost.php [L,QSA]
您可以创建文件/文件夹列表作为匹配条件,甚至可以创建正则表达式模式来匹配多个文件/文件夹。除了最后一个之外,您只需要在后面加上
[OR]
即可。
除了编写“noPost”页面之外,只需直接返回带有 F|forbidden 标志的 403 响应:
RewriteCond %{REQUEST_METHOD} POST
RewriteCond %{REQUEST_URI} ^/dontposthere
RewriteRule .* - [F,L]
您可以使用以下
.htaccess
代码阻止来自所有 URI 的 POST 方法,除了一些可能允许请求您的 POST 方法的特定方法(例如联系表单和登录方式):
RewriteEngine On
RewriteCond %{REQUEST_METHOD} POST
RewriteCond %{REQUEST_URI} !contact.php
RewriteCond %{REQUEST_URI} !login.php
RewriteRule ^ / [F]
希望它对您有用,并为您节省几个小时。