SO actually has a message id and a title in the url, but they will only use the message id. The name is not very suitable, as there may be duplicate messages (same name, different identifiers), name changes, etc. For the url of this question:
/questions/5142095/pretty-urls-for-web-application
To solve the following:
/single.php?id=5142095
The rewrite rule will be:
RewriteRule ^questions/([0-9]*)/(.*)$ /single.php?id=$1
What this means is the beginning of uri (after the domain) ^
, the word questions
, any length of the numbers `[0
It is looking for:
^
start URI- the word
questions
- slash
/
- variable number length
[0-9]*
(for example, 1, 123, 1234, etc.) - slash
/
- any length of any character
.*
(i.e. question title) $
end URI
When a character match (for example, [0-9] * or. *) Is in brackets, mod_rewrite puts the result of the match in a numeric dollar variable. In the above rule, [0-9]*
matches any number (and any length of numbers) and puts it at $1
. The second match .*
Matches any length of any character and puts the match at $2
.
Your application will receive a URI on the right side of the rule, and dollar variables are replaced by matches.
source share