I recently gone through a source code of php and found this code in .htaccess in root
RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^([a-zA-Z0-9-]+)/?$ ?menu=$1 [L,QSA] RewriteRule ^users/([a-zA-Z0-9-/_.[]()@$]+)/$ ?menu=users&user=$1 [L,QSA] RewriteRule ^users/([a-zA-Z0-9-/_.[]()@$]+)$ ?menu=users&user=$1 [L,QSA] RewriteRule ^bans/([a-zA-Z0-9-/_.[]()@$]+)/$ ?menu=bans&banuser=$1 [L,QSA] RewriteRule ^bans/([a-zA-Z0-9-/_.[]()@$]+)$ ?menu=bans&banuser=$1 [L,QSA]
Please,can someone explain me why it is written twice instead once?
Thanks.
Advertisement
Answer
If you look closely at those lines and compare them character-by-character you’ll notice that “doubled” lines actually not identical. I assume that, for example:
RewriteRule ^users/([a-zA-Z0-9-/_.[]()@$]+)/$ ?menu=users&user=$1 [L,QSA] RewriteRule ^users/([a-zA-Z0-9-/_.[]()@$]+)$ ?menu=users&user=$1 [L,QSA]
differ only by /
character to take into account cases when one can query for users/some_user/
and users/someuser
(with or without trailing slash). This is common thing to take into account, although it might be addressed at different levels – webserver or framework ones (or even both).
According to RegExp rules, putting a ?
sign makes the preceding character optional, which allows you to replace those “duplicates” from above with e.g.:
RewriteRule ^users/([a-zA-Z0-9-/_.[]()@$]+)/?$ ?menu=users&user=$1 [L,QSA]
which should be sufficient, but needs testing.