.htaccess Tricks / Directive

Lately I have been more and more forced to use the file .htaccess (file containing apache definitions) or to filter IP-uri (controlled access to a web page) or to set PHP flags.

1. The first thing that is interested in a person working on an online project directly on the server and does not want the site to be accessed by other users besides it, it is IP filtering. In the .htaccess file the following lines will be written:

order deny,allow
deny from all
allow from 83.23.2.4

By this directive The access of all users is blocked (403 Forbidden), less 83.23.2.4. If you work on Locăhost (with Apache installed) and you want the website website/files to be visible only from LAN (Local Area Network), use:

order deny,allow
deny from all
allow from 192.168.0.0/24

2. Blocking access to Webserver for certain IPs/domains.

<limit GET POST PUT>
order deny,allow
deny from 66.57.12.48
deny from .isp.com
allow from all
</limit>

– Access to webserver IP 66.57.12.48 and domain *.isp.com is forbidden.

3. Disable Directory Browsing.

Options All –Indexes

Specifically, access to folders that do not have index (index of /) is forbidden.

4. Changing the default index. (Default page)

DirectoryIndex home.html index.html index.php

A webserver set default will have index.html and index.php index/home files. However, if we want a web address to appear readme.txt as an index file, in .htaccess will be written "directorindex readme.txt".

5. Blocking the websites reference.

RewriteEngine on
RewriteCond %{HTTP_REFERER} site-blocat.com [NC]
RewriteCond %{HTTP_REFERER} site-blocat-2.com [NC]
RewriteRule .* - [F]

If you have a link of your site on other web pages from which you do not want to receive viziers, you can use the above lines.

6. Lock “Hot link” to avoid theft de trafic (bandwidth).

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www.)?domeniul-meu.com/.*$ [NC]
RewriteRule .(gif|jpg)$ - [F]

Forbid another site to use images (JPG and GIF) that are hosted on the server.

7. Change PHP Flags of .htaccess

To be safer, some servers set register_globals Off, however, they are scripts (Drink and Things, for example) that require in installation register_globals on. If you use a Sharehosting and you do not have access to the file php.ini, you can use .htaccess to change php_flags.

php_flag register_globals On

More Definitions for Settings in .Htaccess, Htaccess Cheat Sheet

Passionate about technology, I write with pleasure on stealthsetts.com starting with 2006. I have a rich experience in operating systems: Macos, Windows and Linux, but also in programming languages ​​and blogging platforms (WordPress) and for online stores (WooCommerce, Magento, Presashop).

Home Your source of IT tutorials, useful tips and news. .htaccess Tricks / Directive
Leave a Comment