大网数据广告活动
网站运营与故障问答

Htaccess, deny any request not coming from a form in the website

· 52CE 我爱测

I'm not an .htaccess expert, so I beg your pardon for the basic question. In a website I'm working on there's a form. The action URL of the form is a file placed in the root of the website. I want to deny the direct access to thi…

问题原文

原帖链接 · 3000 · CC BY-SA 3.0

I'm not an .htaccess expert, so I beg your pardon for the basic question.

In a website I'm working on there's a form. The action URL of the form is a file placed in the root of the website.

I want to deny the direct access to this file (from the address bar of the browser, CURL and/or any other way I can't figure out) but the file must still be usable by the form (so the POST request coming from the form must not be blocked).

I tried this extreme approach:

<Files "_mf.php">
Order Allow,Deny
Deny from all
</Files>

What I want to know is if I can exclude any referrer that is not the page where is located the form and if this is a good approach.

已采纳解答

原帖链接 · MrWhite · CC BY-SA 4.0

Just to clarify, there is no fool-proof way to restrict access in this instance (unless this is all behind some kind of authentication) since it needs to be accessible to your form. All you can do is prevent casual browsing. Anyone that really wants to submit requests to this script can do.

<Files "_mf.php">
Order Allow,Deny
Deny from all
</Files>

The problem with this, as you probably found out, is that it blocks all requests, including POST requests from your form submission. What you could do is wrap this in a <LimitExcept> wrapper, so that it applies only to non-POST requests:

<limitExcept POST>
  <Files "_mf.php">
    Order Allow,Deny
    Deny from all
  </Files>
</LimitExcept>

Any GET requests (eg. "from the address bar of the browser") will be blocked (403 Forbidden). As well as any PUT, DELETE, CONNECT, OPTIONS, etc. etc. requests. But POST requests will be allowed through unhindered.

If you want to check the HTTP referer as well, then you could resort to mod_rewrite. For example:

RewriteEngine On
RewriteCond %{REQUEST_METHOD} !POST [OR]
RewriteCond %{HTTP_REFERER} !^http://example.com/
RewriteRule ^_mf\.php - [F]

Where example.com is your canonical hostname. You could instead specify the exact URL of the referring page (the page that contains your form) if you wish:

RewriteCond %{HTTP_REFERER} !^http://example.com/path/to/file-that-contains-form$

This would need to go near the top of your .htaccess file.

What this says is... for all requests to /_mf.php that are either not POST requests or the HTTP Referer is not your domain then serve a 403 Forbidden.

Note that this comes with caveats. It's possible that the user's browser is not set to send HTTP Referers - your script will not be accessible to those users. (This is rare, but some "power" users might choose to do this.)

Any user with CURL know-how can still fake the request and submit requests to your script without accessing your form. Since they can submit a POST request and fake the HTTP referer.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2017-03-27)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。