I have a folder www.mysite.com/page/panel/soascripts/
where there are 10 different PHP files.
I want to prevent access to the folder soascripts and the php files in it. Except X-Requested-With = XMLHttpRequest
(for ajax). Is this possible with htaccess?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
In the htaccess file in your soascripts folder:
RewriteEngine On
RewriteCond %{HTTP:X-Requested-With} !=XMLHttpRequest
RewriteCond %{HTTP:X-REQUESTED-WITH} !^(XMLHttpRequest)$
RewriteRule \.php$ - [L,F]
So without the
X-Requested-With: XMLHttpRequest
request header, the response will be a 403 forbidden.
EDIT:
If you want to add the rules to the document root, you just need to include the path:
RewriteCond %{HTTP:X-Requested-With} !=XMLHttpRequest
RewriteCond %{HTTP:X-REQUESTED-WITH} !^(XMLHttpRequest)$
RewriteRule ^page/panel/soascripts/[^/.]+\.php$ - [L,F]
Make sure to add it before any type of routing rules (like stuff being sent to index.php
).