Apache 的 if_else

最後更新: 2022-11-21




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

regular expression

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


  • 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).
  • ...


[1] Check mulit remote subnet

<If "-R '' || -R ''">


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

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

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

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

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

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

i    Case insensitive regular expression


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


<If "-R ''">
<ElseIf "-R ''">




Creative Commons license icon Creative Commons license icon