Block certain file types with .htaccess?

Hello,

How is it possible to make specific file types inaccessible for direct access using htaccess? I embed some txt files via iframe and include them via PHP. Even when I use "deny from all, allow from 127.0.0.1," the iframe still displays "access denied." What can I do?

(1 votes)
Loading...

Similar Posts

Subscribe
Notify of
5 Answers
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
 Mirko Marek
1 month ago

Hi,

with the .htaccess you can prohibit access to certain file types, but you can no longer include them via Iframe. PHP, on the other hand, can still access the .txt.

That’s what you do.

RewriteEngine On
RewriteCond %{REQUEST_URI} \.txt$ [NC]
RewriteRule .* - [F,L]

😉

Suiram1
1 month ago

If I understand this correctly, the txt’s can be integrated via Iframe, but can not be opened directly in the browser as a tab.

If I understood it correctly, I can only tell you that it won’t really go. Whether a file/side is opened as a browser tab or only integrated via Iframe makes no difference for the server. I’m not sure if Iframe vllt. yet special headers are sent to which you could check in the htaccess, but you could also ask them if available with manual queries.

If you want to prevent the complete access to txt’s, of course that would go.

Suiram1
1 month ago
Reply to  axelred5

So there are always 2 files written, so I could get the 1. Encrypt the file and use the 2nd for the Iframe, that would be a php that I can connect to the login session via a header.

I don’t understand anything from this part. If this is relevant to the problem, it would be nice if you could add a little context there.

However, if the direct access is prohibited to the TXT, can I still use a php nen download?

This can be done very easily that the txt is available by request. PHP allows you to insert a data url instead of the url. This looks like this:

data:text/plain;charset=utf-8,$DeinText

The same goes with binary data like Pdf’s and everything else with nem extra step.

You may need to add the download attribute to the browser the file name and above all that it is a txt.