Imitation of files and directories
The address of your site appears on the user screen simultaneously with design and a content. Therefore the address is a full part of a site. The address such as www.firma.ru (www.firma.gorod.ru), naturally, much better, than www.geocities.com/Gonduras/San-Pedrillio / ~ nasha_firma who argues. And on a question understandable to the person of addresses inside a site the public of a precise consensus yet has not found.
However it would be more pleasant to user to see the address of type/services/special/than/content.phtml? q=e23908a234cc239b3445127.
Lyrical digression. I remember, on Interniti-99 to me have shown flehsh-roller Hewllett Packard Laser Jet 3100. In couple of weeks I have recollected about him and have decided to download it from a house. I for a long time would wander in useless searches on site Leksmark (that you are dared, it and was!), if not their addresses. On HP addresses were understandable - something like "/products/printers/laserjet/3100", and on site Leksmarka was that's it this not clear "q=492898748273". I was in doubts, but a day later recollect, what is it was HP:).
By the way, on this site of the address of releases, versions for a seal and all information pages (the link, files, etc.) virtual, files with such names do not exist.
It is done{made} simply enough. In a file .htaccess lines, for example are written
ErrorDocument 404 all.php
ErrorDocument 403 all.php
ErrorDocument 401 all.php
The file all.php processes a variable $REQUEST_URI and if the necessary information is found, vydet the command
header (" HTTP/1.0 200 Ok ");
It is necessary that browser IE 4 considered, that the page is found, instead of substituted instead of it{her} the service signboard " the address is not found ". In other cases even if "all.php address is requested, the message that the file is not found will be given out to the user.
Certainly, to give out heading and to draw page - simple business. Tracking of results of searches, check on mistakes is more likely routine. The most responsible{crucial} business - analysis of the required address.
Here it is a lot of receptions. For example, at me the version for a seal and page of responses are searched on regular expressions:
if (preg_match (" / (d +)-comment/A ", $url, $res))...
And then from a variable $res [0] I take number{room} of release and I check his presence in base. The address from several subdirectories it is possible, for example, by means of explosion:)
$dir = explode ("/", $url);
And then to process podstroki on one (before explosion it is necessary to clean{remove} from the beginning and the end of a line slehshi).
By the way, before writing a material, I have fairly sent the letter on info@lenta.ru with a question, whether such method of processing of search is used at them or there really there are directories. To me have not answered. I shall not guess, as at them it is made, I shall write, as though I did{made} processing the address.
if (preg_match (" / ([a-z] +) / (d {4}) / (d {2}) / (d {2}) / ([a-z] +)/A ", $url, $match)) {
$request = " SELECT news_id FROM news, rub WHERE news.rub_id=rub.rub_id AND rub_address = ' ". $match [1.] " ' AND news_date LIKE ' ". $match [2.] "-". $match [3.] "-". $match [4.] " ' AND news_address = ' ". $match [5.] "'";
This search is done{made} simply for check, whether there is such news in base. And then depending on result any abuse (or the main page of a heading / site) is given out or page with news, or.
And here is how I check addresses on this site:
if (preg_match (" / (d +)-print/A ", $url, $res)) {
// The version for a seal
}
elseif (preg_match (" / (d +)-comment/A ", $url, $res)) {
// All responses
}
elseif (! preg_match ("/D / ", $url)) {
// The full version of release
}
else {
// Either other headings, or the address it is not found
};
By the way, at myself I as the fair person give out header (" HTTP/1.0 200 Ok ") only if a release / heading are found.
And more one example. We admit{allow}, headings of a site are constructed as a tree, and the table in base looks so:
CREATE TABLE rubrika (
id TINYINT NOT NULL AUTO_INCREMENT,
parent_id TINYINT,
address VARCHAR (16) NOT NULL,
title VARCHAR (128) NOT NULL,
rub_text TEXT NOT NULL,
PRIMARY KEY (id),
UNIQUE address (address)
);
What is title and rub_text - to explain it is not necessary. The field address is an address on which the heading (news will be requested is necessary to make a heading of the first level, and in a field address will be "news"). The field parent_id - the identifier of a heading a level is higher.
Now, if rubiki obviously cannot be lower than the second level, the analysis of the address will not make special work.
$url = $REQUEST_URI;
// We clean{remove} slehshi from the beginning and the end of the address
$url = ereg_replace (" ^ / ", " ", $url);
$url = ereg_replace (" / $ ", " ", $url);
$dir = explode ("/", $url)
// A case when the heading of the second level is requested.
if (sizeof ($dir) == 2) {
// The search uniting the table rubrika with self is made.
// Here it is necessary to notice only, that the table first means a heading of a second level,
// And second, on the contrary, the first.
$request = " SELECT first.id, first.title, first.rub_text FROM
rubrika first, rubrika second WHERE
first.parent_id=second.id AND first.address = ' ". $dir [1.] " ' AND
second.address = ' ". $dir [0.] "'";
// We send search in base, and then we process result.
$result = mysql_query ($request);
if (! mysql_error () ** @mysql_num_rows ($result) == 1) {
}
// In case the search has passed it successfully, but is found nothing
elseif (! mysql_error ()) {
}
//.. And in case there was a mistake.
else
die (" the Mistake of a DB. MySQL writes: ". mysql_error ());
}
// The heading of the first level is requested. Here also there is nothing:)
elseif (! ereg ("/", $url)) {
$request = " SELECT id, title, rub_text FROM rubrika WHERE address = ' $ url ' ";
...
};
Examples will suffice? Further - business of imagination. We shall sum up, we shall paint with the positive and negative moments.
Pluss
* Beautiful addresses, an opportunity to go in a heading, having typed{collected} its{her} address on the keyboard. Gratitude from fans of the keyboard.
* Reduction of quantity{amount} of files, reduction of quantity{amount} of repeating operations in different files.
* Centralization of a conclusion. Gathering of the majority of operations in a uniform point of an input{entrance}.
* Concealment of some technological part of a site.
Minuses
* Increase resursoemkosti at the expense of check of the address and compilation of the big file instead of several small.
* Complexity with introduction of new parameters (I, was possible to say, it was successfully turned out with the version for a seal, but it would be more logical to see addresses of type/13/print). Something should be dumped{reset}, for example in kuki.
* Something, for example, search, and remain outside of " a point of an input{entrance} " (though... " How IT works " does{makes} search in the address, but it will be inconvenient for more-less complex site or impossible).
* Additional complexities with addresses of pictures and navigation on a site (the browser will measure all addresses concerning the open document, let even from the nonexistent address).
At last: in this release I used a heap of regular expressions, therefore (and at the request of readers) I promise shortly to mention this subject.
Imitation of files and directories. A part 2
In the last release I have described job with CHPU (" chelovekoponjatnye URLy ") through a mistake *404. Itself I use this circuit. But a day later after the publication of a material the response was written by Konstantin Shevchenko (aka cat) and has suggested me to tell to public about other modes of work with addresses on a site. He also advised me.
In responses as have written, it is what is it serious gruzit system. Yes, gruzit. And I about it have fairly written. And more one amendment: dialogue in responses
The visitor: And it is interesting, what will say poiskoviki on etot imaginary ErrorDocument?
I: Poiskoviki count such addresses normal - they divide all on 200 Ok and 404 Not Found. And the rest him on a drum.
Poiskoviki, certainly, do not divide all on 200 and 404. They know also other codes of the server, nobody can simply check up at a level of programs, whether there is a document which has sent the server whether or not. If the code 200, means exists and is accessible. And anything more.
Then JUra Drills has written concerning predyduego a material in "Ezhenedel`kakh". However, not everything, that it is possible to write it, how he thinks.
One thing which has forgotten to describe in the previous release - virtual file archive. Through ErrorDocument it is quite possible to trace searches to a nonexistent directory download and to give out the requested files from base, and the manager of a site could work with this archive through the web - form. Correctly to give out type of a file (content-type), it is necessary to take it when it zakachivaet the manager. In the table of files to make an additional field in which to store{keep} these types, and to give out them in heading. Certainly, there will be a decline of productivity of the server.
And now that has described Kostja. I shall list ways on increase of complexity (the order, however, disputable).
The server searches for a file with the same name
It appears, enough to register in installations of a directory (httpd.conf or .htaccess) line Options Multiviewsili if Options directive already to eat, add MultiViews to her. Then if the user types{collects} " <the address of a directory>/foo/bar ", the server will search for a file with a name "foo" and with any expansion. Found with the greatest concurrence (for me a riddle) a file he will process it with his type mime that is if is news.php, and news address / the server will give the address on processing php is typed. If it is a picture the server will give its{her} browser as a picture (having sent sootvestvujuhhij heading content-type). And in news.php we assort $REQUEST_URI. For example, if news are deduced by the whole tape, or for the certain date, analysis it is possible to make such:
/* The first variant - when is typed the address such as "/news/010120", it is possible with fraction on the end.
// Symbols ^ and $ here designate binding to the beginning and the end of a line. Podstroka [0-9] {6}
// Means 6 figures (if at you news can be dated 1999 year and earlier,
// Use addresses with a full format of year and 8 figures instead of 6). */
if (ereg (" ^/news / ([0-9] {6}) $ ", $REQUEST_URI, $match) ||
ereg (" ^/news / ([0-9] {6}) / $ ", $REQUEST_URI, $match)) {
}
/* The second variant - is typed the address simply "/news" or "/news / " */
elseif (ereg (" ^/news / $ ", $REQUEST_URI) || ereg (" ^/news $ ", $REQUEST_URI)) {
}
/* Searches to all other addresses (in this file) are considered as attempts of breaking of a site */
else
die (" Error 404 Not found ");
The same can be made, for example, with the catalogue of production of firm - to bear{take out} all this put in a separate file catalogue.php, and addresses to make a kind "/catalogue/rubrik1/rubrik2/rubrik3". Thus in a file catalogue.php the beginning of a line will be bitten off, and it is possible to process by a principle described in the previous release further. The rest can be sent, besides, in ErrorDocument.
The server assorts search
Method similar, but on vskidku less resursoemkij because it is not necessary to search for files on a directory.
In installations of a directory (besides httpd.conf or .htaccess) we write:
<FilesMatch " ^ (news) $ ">
ForceType application/x-httpd-php
</FilesMatch>
In a directory the file with a name "news" ("news", without expansion) lays. When "/news address, or "/news/bla-bla-bla" is requested, the server carries out a file news as a php-script. And inside him processing of a variable $REQUEST_URI is made.
To not write for each similar file the block FilesMatch, it is necessary to change a line of a pattern a little. Let the server searches for files without expansion, that is what in a name do not have point:
<FilesMatch " ^ ([^]. +) $ ">
Very conveniently! I shall sometime put same and.
The server copies searches
Very useful thing mod_rewrite. It is possible to make her all above described, and it is a lot of another.
Unfortunately, my fight with mod_rewrite has not crowned success (Kostja writes, that described below is enough for job Rewrite Engine under Unix. At me under win98 - in any...). Therefore I describe obvious things and that has described Kostja.
For the beginning it is necessary raskommentirovat` a line
LoadModule mod_rewrite <a way to a module / name of a file>
In httpd.conf. To configurations of a directory we write a line " RewriteEngine On ". Then - RewriteRule command: RewriteRule <pattern> <replacement>
For example RewriteRule ^ (. *).html $/otherdir / $ 1.html (all without inverted commas)., actually, and all. Everything, that I and could not check up: I have asked at jasenja, I have asked a poplar, I have asked a forum... The forum has not answered me. I (am melodious) shall ask public... Just in case, I ask dear public: how to start Rewrite Engine under win98 se + apache/1.3.14 + php/4.0.4-Antonio (it is established as the module)?
For now one more example (besides from Shevchenko):
RewriteEngine On
RewriteRule ^ (. *).htm $/portal / $ 1
<FilesMatch " (portal) $ ">
ForceType application/x-httpd-php
</FilesMatch>
There one file with a name "portal", in which perenapravljajutsja all searches to html-files in the given directory lays. Also it turns out, as though files there lay.
At last we shall recollect Lentu.ru.
Here, in sa-b-amom end Nosik speaks about resursoemkost` their technologies. " The publishing machine written by Maxim Evgenjevichem Moshkov, (which library) is interesting to that, that she weighs about{near} 60Kb "
I do not know, that from itself this system represents (most likely, skomnilirovannyj), I shall not guess. I would like to estimate, as dynamic addressing it is possible reaizovat` through php. However, here not in him business - would be the Apache. So, the circuit of addresses <heading> / <year> / <month> / <day> / <news>. If to cut off a name of news, we shall receive materials of a heading for a day. If to cut off day, month and year, we shall receive last materials of a heading.
RewriteRule ^ ([a-z] +) / $ rubika_last / $ 1
RewriteRule ^ ([a-z] +) / ([0-9] {4}) / ([0-9] {2}) / ([0-9] {2}) / $ rubrika_date / $ 1 / $ 2-$ 3-$ 4
RewriteRule ^ ([a-z] +) / ([0-9] {4}) / ([0-9] {2}) / ([0-9] {2}) / ([a-z] +) / $ rubrika_news / $ 1 / $ 2-$ 3-$ 4 / $ 5
<FilesMatch "^rubrika">
ForceType application/x-httpd-php
</FilesMatch>
Advantages of this method in comparison with a uniform point of input{entrance} EerrorDocument are obvious: the cursor php interprets only that will be carried out. Any switch/case or if/elseif/else, any superfluous lines.
The everything else - we give ErrorDocument " as in the previous problem ".

|