Apache 的 if_else

最後更新: 2022-11-21

目錄


if

 

Unary operators

  • -d     True if the file exists and is a directory
  • -e     True if the file (or dir or special) exists
  • -f     True if the file exists and is regular file
  • -s     True if the file exists and is not empty
  • -L     True if the file exists and is symlink
  • -h     True if the file exists and is symlink (same as -L)
  • -z     True if string is empty
  • -n     True if string is not empty
  • -R     Same as "%{REMOTE_ADDR} -ipmatch ...", but more efficient

binary operators

  • ==
  • !=
  • >
  • >=
  • <
  • <=

====

  • -ipmatch
  • -strmatch
  • -strcmatch  # same as -strmatch, but case insensitive
  • -fnmatch     # same as -strmatch, but slashes are not matched by wildcards

Integer Comparison

  • -eq
  • -ne
  • -lt
  • -le
  • -gt
  • -ge

regular expression

  • =~         String matches the regular expression
  • !~         String does not match the regular expression

Functions

  • req        # Get HTTP request header; header names may be added to the Vary header, see below
  • tolower  # Convert string to lower case
  • md5      # Hash the string using MD5, then encode the hash with hexadecimal encoding
  • file        # Read contents from a file (including line endings, when present)
  • reqenv   # Lookup request environment variable (as a shortcut, v can also be used to access variables).
  • ...

Example

[1] Check mulit remote subnet

<If "-R '192.168.0.0/16' || -R '10.0.248.0/24'">

[2]

<If "md5('foo') == 'acbd18db4cc2f85cedef654fccc4a4d8'">
  Header set checksum-matched true
</If>

[3] Check a HTTP header for a list of values

<If "%{HTTP:X-example-header} in { 'foo', 'bar', 'baz' }">
    Header set matched true
</If>

[4]Cache 了的檔案(IMG)不被 php call

<If "%{REQUEST_URI} =~ m#/.*\.php/.*\.(jpg|png|css|js)$#i">
    Require all denied
</If>

m#regexp#i is an alternative of /regexp/

i    Case insensitive regular expression

 


If ... ElseIf ... Else ...

 

<If "-R '10.1.0.0/16'">
  #...
</If>
<ElseIf "-R '10.0.0.0/8'">
  #...
</ElseIf>
<Else>
  #...
</Else>

 

 

 

Creative Commons license icon Creative Commons license icon