.htaccess 阻止本地服务器上的外部访问,某些 URL 除外

.htaccess block outside access on local server except for certain URL's

我目前在 MAMP 服务器上设置了本地 .htaccess 以阻止来自本地系统外部的所有传入流量;

<FilesMatch ".*">
Order deny,allow
Deny from all
Allow from 127.0.0.1
</FilesMatch>

这很好用,但我随后使用 API 之类的 PayPal,它需要访问您的网站以获得 IPN。是否可以保留对网站其余部分的限制,只允许外部访问特定的 url,如 https://example.com/paypal_ipn

我知道我可以在使用 IPN 时关闭限制,但这不是我想要的。非常感谢。

您可以在根 .htaccess 中使用基于 mod_rewrite 的规则:

RewriteEngine On

RewriteCond %{THE_REQUEST} !/paypal_ipn [NC]
RewriteCond %{REMOTE_ADDR} !^127\.0\.0\.1
RewriteRule ^ - [F]

这将阻止所有不属于以下内容的请求:

  1. 源自本地主机 (127.0.0.1)
  2. 对于/paypal_ipn