Показать сообщение отдельно

  #3  
Старый 19.03.2007, 00:58
Дрэгги
Moderator - Level 7
Регистрация: 26.08.2005
Сообщений: 457
Провел на форуме:
4007686

Репутация: 1442
По умолчанию

Let's have a look at another example (here I already use relative links), let's make out of this

Цитата:
http://site.com?index.php?forum=someshit&nickname=hacker& userID=31337
rewriting it into

Цитата:
http://site.com/someshit/31337-hacker.htm
Firstly, as I have mentioned above, enable

PHP code:

Цитата:
Options +FollowSymLinks
then


PHP code:

Цитата:
RewriteEngine On
RewriteRule ^([^/]+)/([0-9])-(.+).htm$ index.php?forum=$1&nickname=$2&userID=$3
First line is understandable let's have a closer look into the second one. ([^/]+) - before "slash"
we allow the use of any symbols (imagine we will have a digit in our forum title...though something like ([a-zA-Z0-9]+) is possible, but here however the dash won't be displayed).

After ([0-9])-(.+) that is, in the second part of the "final file" we insert the variable $2 itself (which is an equal to "hacker"), and in the first one we insert only digits (in our case it is, for instance, the user ID which is an equal to 31337 ( ([0-9]) ). And certainly we add an extension in the end. The second part of the line needs no comments. . i.e there it's only indicated what is to rewrite. All three variables' values.

I'll go over explaining the regular variables themselves once more:


Цитата:
[a-z] - the lower Roman letters

[A-Z] - the capital Roman letters

[0-9] - digits

by the way... you may also use [c-y] or [4-8] that is any letters/digits range.

^ - it has two ways of use.

1) when it is inside of the square brackets it means the denial, i.e [^ab123] means that the replacement will take place with the use of any symbols except a,b,1,2,3


2) means the start of the "regular" thing (Ex. RewriteRule ^ /*expression*/)


dot is any kind of the single symbol

+ - the use of the one or more than one symbol and may be used only with [blablabla] (Ex. [0-9+] that is the rewrite will be processed by one or more symbols from 0 to 9)

? - the same things as "+", however none at all or just one indicated symbol is used.


* - rather ineffective variable from my point of view. But still its point is figuratively speaking to replace with whatever comes to hand )


$ - the end of the line. In general it works without it nonetheless.


% - sets the variable incoming from from RewriteCond (keep reading about it further below)


In addition to this the RewriteCond may be used, that is not the rule of the rewrite is set, but the CONDITION (!!!).
They have similar syntax.


what we can do to them... let's look into it using this example:


PHP code:

Цитата:
RewriteRule ^(forum)/(topicID)$ /read-$1-topic-$2
Moreover, this arises


PHP code:

Цитата:
read-#forum-and-#topicID

where #forum is forum's title, #topicID is the ID of the topic.


All the variables are given number in accordance with their appearance and the condition and the rule both have 2 variables (in this particular example naturally they have, but generally speaking even one hundred if you like, no one prevents you from doing this), which are being set by round brackets, because in order to use them you'll need to put them in there where they are exactly needed by you in the final link-result.


Let's combine both the rule and the condition:

Цитата:
RewriteCond %{CONDITION_STUFF} ^(forumname)/directory/(topicID)
RewriteRule ^(forumname)/directory/(topicID)$ /our_forum-$1-topic-%2

By this we'll rewrite the link with the numerous parameters into the following type

PHP code:


Цитата:
forumname-forum-topicID

In general it'll do even without "slashes", but IMHO the inscriptions will merge = )


Here I've had in mind quite funny regular thing like this:


PHP code:


Цитата:
RewriteRule ^([^.]+)$ /index.php

Don't you find it a little weird upon reading? = ) You're right...it rewrites everything based on a rule everything except any kind of symbol, I myself haven't tried it yet, but whoever checks what apache will spit out upon this one please ..requesting to write it here = )

So.. We've learnt how to use RewriteRule and RewriteCond, using mod rewrite in order to provide the safety of the server (for example from sql inj) and simply using it in order to decorate and memorize the links.

Thank you for reading

2nd part coming up

Original copyright: blackybr

Последний раз редактировалось Дрэгги; 19.03.2007 в 01:01..