Hello.
I wanted to share with the general reader information about the implementation of the Friendly URL on my projects.
The reasoning will be about ergonomics, syntactic principles, clarity and convenience of interfaces. I warn you, on the part of the software implementation, I will not develop this topic, because everyone does it differently and it’s quite difficult to find the truth here. Someone can compare the resulting URL with a bunch of rules, someone can index coincidences, someone, if you like, generate a .htaccess file with the rules. Not the point.
Types of approaches
The first thing I would like to say is about the difference between projects that work only through the Friendly URL, and work both on direct paths with parameters and on the Friendly URL.
')
The implementation of the second option is clear. If physically there is no requested file, then send search among the rules for a match. The advantage of this option is that it is easy to implement and can be entered into the project in chunks where it is needed.
But the second option is also possible, when direct addressing to scripts is completely prohibited. It is only, for example, for the static / folder, which stores scripts and images. Access to everything else is possible only through the set of rules for this project. The advantage is hiding the real file structure of the project and opening only those pages that are allowed.
Syntax Human-URLs
I will not argue on the topic of "How is better," because on this occasion, people have repeatedly voted on Habré (
http://habrahabr.ru/blogs/searchengines/125103/ ,
http://habrahabr.ru/blogs/personal/ 41602 / ,
http://habrahabr.ru/blogs/personal/35599/ ) I will tell only about my own concept:
- If there are several language versions on the project, the language is displayed as the first component of the URL (site.com/ru/, site.com/en/). Perhaps, of course, the division of language versions by domains (site.ru, site.ua, site.kz), but it seems to me that this is more suitable for defining regional representative offices of a certain project than language affiliation.
- If the main content of the page is a list of some objects to which the transition is possible, we represent the URL of this page as a directory (site.ru/news/, site.ru/users/, site.ru/catalog/auto/trucks/used/). It is important to note that if a page is a list of elements to which there is no transition (for example, the list of addresses of dealers), then it is not necessary to represent its URL as a directory. This is the final page in the hierarchy. Also note that you should not insert slashes where there are no intermediate lists, that is, if you cut from the end of the component from the end of the component to the nearest slash, there must always be a page corresponding to the URL received.
- If the main content of the page is the final step of a certain hierarchy or just an independent page, it is reasonable to end its URL as the name of a certain file. I prefer .html, although why not .php, just less confusion. Examples: /about.html, users / anufry.html, /news/olimpia-2011.html, /catalog/auto/trucks/used/438829.html.
- For large projects, it makes sense to split sections of the site into subdomains, such as blogging to a separate subdomain. I think this is beautiful and justified, especially if, at the same time, a modified design and its page structure are provided for blogs. Maybe someone will say "Why not on a separate domain?". Well, I will answer - so as not to suffer with the cookies, not to create the sensation of moving through the advertising link.
- In connection with points 2 (directories) and 3 (end pages), it is highly undesirable to put parameters such as order, limit, page, various specifying parameters, etc. into the path. The URL of the form / catalog / brushes / last_added / page3 / new | red | classic / ... creates a frightening feeling that you have gone somewhere where you can’t get out. Let these parameters be where they are supposed to be - in a GET request. / catalog / brushes /? order = last_added & page = 3 & specify [color] = red & specify [style] = classic. This is certainly longer, but it is clearly visible that the content of the page is not displayed by default, but according to certain parameters, and you can get rid of them just by taking the path from the URL.
- Forms. If you are using the MVC architecture, where all forms are processed by the same controller as the built form, the optimal attribute is specified action = "" (empty). This implies that no matter how you change the Friendly URL for a given form, the data will go where it should be, respectively, fewer edits in the templates, etc.
Router
Since the construction of the URL is often not done by the developer, the question arises of a convenient means for creating rules by man, with a minimum of knowledge about the system structure and the list of valid parameters.
For this I use the so-called Rawter. From the user's point of view, this is a rule constructor with a clear and user-friendly interface. It is a table with input and select fields, each of the rows of which has the following parameters:
- Subdomain - Actually the third level domain, if we need it. It is usually empty.
- Rule - The rule for matching. In the rules, I allow the introduction of variables that match the following patterns in the regular expression:
- {abc} => [A-Za-z] +
- {123} => [0-9] +
- {word} => [A-Za-z0-9 _-.] +
- {lang} => [az] {2}
- {chars} =>. *
Each of the variables is memorized and can be substituted in the "Variables" field in the form of $ 1, $ 2, $ 3, and so on in the order in which the rule follows. - Frame or Subject - Select list. Determines which graphic theme will be applied on this page. This is usually the default framework.
- Plugin - Select list. In my system, plugins are MVC structures, so in essence, this is an indication of the controller that will take over the work.
- Event - Select list. The method in the controller to be invoked.
- Variables - Several inputs, of the form "variable name = ________". Valid variables in this event (method). Information about permissible variables is stored in the configuration file, so the person creating the rule can only specify values for permissible variables.
As a result, we assign the following responsibilities to the user of Router.
- Come up with a unique and logical rule
- Correctly place it in the list so that it is not blocked by another rule.
- Do not confuse the order of variable values from the rule when they are inserted into the "Variables" column
- Knowledge of English (I believe that the URL should be in English so far, not in transliteration and not in Russian / Albanian, but this is purely my opinion)
Thank you for your attention, I hope someone helped my experience, and someone pushed for new ideas.