1

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?

2
  • 1
    Why bother? Are people likely to stumble across it and be confused? Adding an X-Requested-With header is trivial if any attacker wants to do something nefarious with the data. Commented Jan 13, 2014 at 17:04
  • 2
    Well, I just want it. In my logs I already saw some accesses. I don't want it as a single security option, I already implemented an authorization secr. (I know that an attacker can easily manipulate the header) but I want this for regular users. They should not stumble in this directory. Commented Jan 13, 2014 at 17:09

1 Answer 1

2

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).

Sign up to request clarification or add additional context in comments.

2 Comments

JonLin Thank you. @Quentin I don't have a lot of directories or files. I just want to prevent anyone from display-raping the php files and I don't know how else to archive this. hm, I've read the article.... will this also slow down my php mysql queries to my database in the soascripts folder?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.