A few words about how it is possible to use mod rewrite in order to increase the security of a site, as well as for content, access rights limitations etc. and so on (without using web programming languages).
What else is mod rewrite capable of? Well it can do quite a lot of things. As I mentioned above,
RewriteCond
determines the rules of a condition and
always
preceds the
RewriteRule , i.e the RewriteRule in this case works only under a true condition. Rewrite module accepts
server variables as well which is undoubtedly pleasing. )
Here are some of them which have to be used quite often:
HTTP_USER_AGENT - the user agent (for instance Opera/9.10 (Windows NT 5.1; U; ru))
HTTP_REFERER - referer (i.e., a site which you came from to an original, final site)
HTTP_COOKIE - cookies being transferred =)
HTTP_FORWARDED - the address of the user who is currently on the page
HTTP_HOST - the address of a site (without http: //)
HTTP_PROXY_CONNECTION - set in the event when the client has come via "transparent proxy-server
HTTP_ACCEPT - a more precise definition of the informatio type (the so-called media-types) accepted by a browser on the given page, for example text/plain, text/html, image/gif, image/jpeg)
REMOTE_ADDR - your ip-address
REQUEST_METHOD - a method used for the delivery of the inquiry (GET, PUT, POST and so on)
SCRIPT_FILENAME - a full way to an executable script on the server
PATH_INFO - everything following the name of a script being initiated
QUERY_STRING - the line of an inquiry
Let's assume that we have certain site: http://site.com
For some reason we totally don't want certain someone to have access to some file type, for instance, pictures jpg, png (well meaning that only scripts could do it). It is possible to try to do it by means of mod_access, something having .htaccess of sorts in its content root:
PHP CODE:
<FilesMatch "\.(jpg|png)$">
Order Deny,Allow
Deny from all
</FilesMatch>
Well, the defect here is that it will forbid the access for both you and scripts triggering/opening pictures. We do it with the help of mod_rewrite:
PHP CODE:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_REFERER} .
RewriteCond %{HTTP_REFERER} !^http://(www\.)?site\.com(/.*)?$
RewriteRule .(jpg|png)$
First two lines here are clear. Further along we see a condition - when HTTP_REFERER contains site.com (regular variables see above) then the viewing of a picture is allowed.
When can it be helpful? Well let's suppose we have a photoarchive and there is a counter and we wish to know the exact number of visitors. But actually when the photo is indexed in a search engine, and the person follows the link from that same yandex - in "referer" the yandex itself is indicated and it's not for the fact that the link will be directly on a picture. The so-called hot-linking. I think it is understandable
Now let's make an authorization of sorts. Using Mod_rewrite again of course..
Since I have already told that we shall not use php, perl etc. we will have to manage without the database as well =)
For this purpose let us investigate one more directive of Mod - the RewriteMap which is used in the rules concerning the replacement by different mapping functions relative to different correspondence areas with the help of the rewrite rule via rewritecond..
The authorization we will process through the sessions file in auth.txt which is situated outside of WWW. Let us assume that the full way on the server to the site is /home/user/www/
Let's create a file /home/user/auth.txt having this kind of content:
PHP CODE:
# session 1
abcdefghijkl 1
Moving further along let's see what we will have inside the .htaccess
PHP CODE:
Options +FollowSymLinks
RewriteEngine On
RewriteMap sessionids txt:/home/user/auth.txt
# there is no session at all
RewriteCond %{QUERY_STRING} !^(.*&)?sessionid= [NC,OR]
RewriteCond %{QUERY_STRING} ^(.*&)?sessionid=(&.*)?$ [NC]
RewriteRule .* - [F,L]
# there's no session in the file
RewriteCond %{QUERY_STRING} ^(.*&)?sessionid=([^&]+)(&.*)?$ [NC]
RewriteCond ${sessionids:%2|0} ^0$
RewriteRule .* - [F,L]
or for a file looking like (let's use a more secured version - session + IP)
PHP CODE:
# session-ip 1
blablbalbalb-127.0.0.1 1
we'll get a slightly different result
PHP CODE:
RewriteCond %{QUERY_STRING} ^(.*&)?sessionid=([^&]+)(&.*)?$ [NC]
RewriteCond %2-%{REMOTE_ADDR} ^(.+)$ RewriteCond ${sessionids:%1|0} ^0$
RewriteRule .* - [F,L]
Everything which is done here I have already described. I'll repeat one more time -
We take the QUERY_STRING (i.e. the line of an inquiry) and verify whether it has the session through the .txt file (the same can be done through the .php file addressing the database which will yield the same results).
RewriteCond%2-%{REMOTE_ADDR} ^(.+)$ - %2 is taken from the first RewriteCond, our session at first, then the variable of the session and the IP-address (we have it in the .txt file too) - all of it we include in one variable - %1
RewriteCond ${sessionids:%1|0} ^0$
${sessionids:%1|0} - the verification on RewriteMap
sessionid - the name of the Rewrite Map itself
%1 - the variable which I have already mentioned above
0 - just the default variable
The gist of it is that in case the verification on the file returns the positive result and the line with the data exists for the Rewite Map in question, then 1 is returned (that exact "one" I put in the end of every line in the file) auth.txt, in the opposite case 0 is returned ( ^0$ means something like "let's associate 0 with it"). If the verification didn't take place at all, then still the same - 0 is returned, after which mod rewrite goes on with its work, having sent Forbidden ( [F] ) first.
(c) blackybr
Also see http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html
http://en.wikipedia.org/wiki/Rewrite_engine
English version translation: Дрэгги