106

I need to change my .htaccess and there are two lines which I don't understand.

RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

When I should use these lines ?

Kev
  • 115,559
  • 50
  • 294
  • 378
yossi
  • 1,401
  • 2
  • 11
  • 16

3 Answers3

230

Not the place to give a complete tutorial, but here it is in short;

RewriteCond basically means "execute the next RewriteRule only if this is true". The !-l path is the condition that the request is not for a link (! means not, -l means link)

The RewriteRule basically means that if the request is done that matches ^(.+)$ (matches any URL except the server root), it will be rewritten as index.php?url=$1 which means a request for ollewill be rewritten as index.php?url=olle).

QSA means that if there's a query string passed with the original URL, it will be appended to the rewrite (olle?p=1 will be rewritten as index.php?url=olle&p=1.

L means if the rule matches, don't process any more RewriteRules below this one.

For more complete info on this, follow the links above. The rewrite support can be a bit hard to grasp, but there are quite a few examples on stackoverflow to learn from.

SherylHohman
  • 14,460
  • 16
  • 79
  • 88
Joachim Isaksson
  • 170,943
  • 22
  • 265
  • 283
  • QSA replaces `?` to `&`, making it impossible to distinguish between `/page&foobar` vs `/page?foobar`. How can I stop QSA from replacing `?` to `&`? – Pacerier Sep 27 '17 at 06:04
  • 4
    I believe to remember all those things you can make an association like: `QSA` stands for Query String Append; `L` stands for last (means execute and stop looking for the next coincidence) – Utmost Creator Sep 04 '20 at 17:56
  • -d directory .. -f file – mercury Nov 07 '21 at 22:25
  • L is for "last" - while your explanation is perfectly fine, it makes understanding it much easier if you give this memory hint – clockw0rk Feb 17 '22 at 11:55
11

If the following conditions are true, then rewrite the URL:
If the requested filename is not a directory,

RewriteCond %{REQUEST_FILENAME} !-d

and if the requested filename is not a regular file that exists,

RewriteCond %{REQUEST_FILENAME} !-f

and if the requested filename is not a symbolic link,

RewriteCond %{REQUEST_FILENAME} !-l

then rewrite the URL in the following way:
Take the whole request filename and provide it as the value of a "url" query parameter to index.php. Append any query string from the original URL as further query parameters (QSA), and stop processing this .htaccess file (L).

RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

Apache docs #flag_qsa

Another Example:

RewriteRule "/pages/(.+)" "/page.php?page=$1" [QSA]

With the [QSA] flag, a request for

/pages/123?one=two

will be mapped to

/page.php?page=123&one=two

antelove
  • 2,612
  • 20
  • 19
0

This will capture requests for files like version, release, and README.md, etc. which should be treated either as endpoints, if defined (as in the case of /release), or as "not found."

Pyae Sone
  • 1,436
  • 2
  • 13
  • 18