Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is L in [QSA, L] in htaccess

Tags:

.htaccess

qsa

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.

Hi, what are some easy examples to explain the use of L? I can't seem to grasp this explanation above. Any help will be highly appreciated. Thanks.

like image 207
stackoverflower Avatar asked May 09 '13 18:05

stackoverflower


People also ask

What is IfModule Mod_rewrite C?

The <IfModule mod_rewrite. c>... </IfModule> block ensures that everything contained within that block is taken only into account if the mod_rewrite module is loaded. Otherwise you will either face a server error or all requests for URL rewriting will be ignored.

What means in htaccess?

htaccess is a plain text configuration file for the Apache HTTP Server that allows administrators to specify options for directories where web content is served. The initial "." in . htaccess means that the file will be invisible on Unix-like systems in directory listings except with the "ls -a" command.

What is Rewriterule?

L|last. The [L] flag causes mod_rewrite to stop processing the rule set. In most contexts, this means that if the rule matches, no further rules will be processed.


2 Answers

.htaccess flag list

  • C (chained with next rule)
  • CO=cookie (set specified cookie)
  • E=var:value (set environment variable var to value)
  • F (forbidden - sends a 403 header to the user)
  • G (gone - no longer exists)
  • H=handler (set handler)
  • L (last - stop processing rules)

Last rule: instructs the server to stop rewriting after the preceding directive is processed.

  • N (next - continue processing rules)
  • NC (case insensitive)
  • NE (do not escape special URL characters in output)
  • NS (ignore this rule if the request is a subrequest)
  • P (proxy - i.e., apache should grab the remote content specified in the substitution section and return it)
  • PT (pass through - use when processing URLs with additional handlers, e.g., mod_alias)
  • R (temporary redirect to new URL)
  • R=301 (permanent redirect to new URL)
  • QSA (append query string from request to substituted URL)
  • S=x (skip next x rules)
  • T=mime-type (force specified mime type)

Flags are added to the end of a rewrite rule to tell Apache how to interpret and handle the rule. They can be used to tell apache to treat the rule as case-insensitive, to stop processing rules if the current one matches, or a variety of other options. They are comma-separated, and contained in square brackets.

like image 150
Smit Patadiya Avatar answered Sep 17 '22 11:09

Smit Patadiya


The QSA flag means to append an existing query string after the URI has been rewritten. Example:

URL=http://example.com/foo/bar?q=blah

Rule:

RewriteRule ^foo/(.*)$ /index.php?b=$1 

Result=/index.php?b=bar

Notice how the q=blah is gone. Because the existing query string is dropped in favor of the one in the rule's target, (b=$1). Now if you include a QSA flag:

RewriteRule ^foo/(.*)$ /index.php?b=$1 [QSA] 

The result becomes=/index.php?b=bar&q=blah


The L flag simply means to stop applying any rules that follow. Given the same URL, http://example.com/foo/bar?q=blah, and given the rules:

RewriteRule ^foo -   RewriteCond %{REQUEST_URI} !^/bar.php RewriteRule ^(.*)$ /bar.php?z=$1  

The first rule gets applied and the URI gets passed through unchanged (via the - target). The rewrite engine then processes the next rule, and the URI gets rewritten to /bar.php?z=foo/bar. What happens when you add an L to the end:

RewriteRule ^foo - [L]  RewriteCond %{REQUEST_URI} !^/bar.php RewriteRule ^(.*)$ /bar.php?z=$1  

The URL http://example.com/foo/bar gets passed through untouched from the first rule, then stops because of the L flag. If the URL is http://example.com/something/else then the first rule doesn't match and the second rule gets applied, rewriting the URI to: /bar.php?z=something/else

Note that since the rewrite engine loops through all the rules until the URI stops changing, the L flag will not prevent the looping, only any further rules from getting applied in the current iteration.

like image 25
Jon Lin Avatar answered Sep 16 '22 11:09

Jon Lin