3

I am learning to create a REST api from a tutorial. In the tutorial there was one step which performs URL rewriting. The code is as follows:

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.*)$ %{ENV:BASE}index.php [QSA,L]

Can anybody please explain rewrite condition and %{ENV:BASE} part in it ?

Tejas
  • 585
  • 3
  • 15

2 Answers2

2

The condition is simple:

If requested URI does not indicate to any existing file then redirect to ${ENV:BASE}index.php.

You can find description of RewriteRule flags here. Briefly speaking:

QSA stands for "query string append". It means that query string will be appended to replacement URI.

L means that is the last rule and no further rules will be processed.

On this page there is a good explanation for environment variables in htaccess:

The %{ENV:variable} form of TestString in the RewriteCond allows mod_rewrite's rewrite engine to make decisions conditional on environment variables. Note that the variables accessible in mod_rewrite without the ENV: prefix are not actually environment variables. Rather, they are variables special to mod_rewrite which cannot be accessed from other modules.

So, you can use environment variable in your rule like this ${ENV:<name>}. The variable has to be set earlier, of course.

rzymek
  • 846
  • 6
  • 19
0

${ENV:<name>} is used for avoiding repeated coding. Some conditions would be stored as variable and could be used in RewriteRules. For more details you can see this question.

Community
  • 1
  • 1
Huseyin
  • 1,407
  • 2
  • 24
  • 38