Detect Apache version in apache config?

后端 未结 5 468
醉酒成梦
醉酒成梦 2020-12-30 00:48

tl;dr: How do I do the following in a .conf or .htaccess file:


  # Do A


  # Do B
&         


        
相关标签:
5条回答
  • 2020-12-30 01:08

    You can run different versions of Apache on the same host. Q: What's wrong with separate config files in separate directories? I honestly believe that's probably the cleanest approach...

    Nevertheless, Apache .conf files allow an <IfDefine>, which you can specify at runtime with "-D":

    http://httpd.apache.org/docs/2.0/mod/core.html#ifdefine

    0 讨论(0)
  • 2020-12-30 01:20

    Assuming you have mod_version installed (many distributions ship it by default) you can do the following:

    <IfVersion >= 2.4>
        # Require ...
    </IfVersion>
    <IfVersion < 2.4>
        # Order ...
        # Deny ...
        # Allow ...
    </IfVersion>
    
    0 讨论(0)
  • 2020-12-30 01:25

    I ran into this problem because FallbackResource is not in early versions of Apache and often clever hosting companies remove mod_rewrite once they have a version of Apache with FallbackResource. I use the following .htaccess when I want to put library code up that adapts to its environment:

    <IfModule mod_rewrite.c>
        RewriteEngine on
        RewriteRule ^ - [E=protossl]
        RewriteCond %{HTTPS} on
        RewriteRule ^ - [E=protossl:s]
        RewriteRule "(^|/)\." - [F]
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_URI} !=/favicon.ico
        RewriteRule ^ index.php [L]
    </IfModule>
    
    <IfModule !mod_rewrite.c>
        FallbackResource index.php
    </IfModule>
    

    Now of course there are some Apache versions / setups where this fails - but if mod_rewrite is there, use it and if not, hope that FallbackResource is there.

    0 讨论(0)
  • 2020-12-30 01:33

    You could bypass the need to know by using mod_rewrite for your access control:

    RewriteEngine On
    RewriteCond %{REQUEST_URI} !\.(long|list|file|types)$
    RewriteRule .* - [F,L]
    
    0 讨论(0)
  • 2020-12-30 01:35

    A hack that I have used.

    # Apache 2.2
    <IfModule !mod_authz_core.c>
        Satisfy Any
    </IfModule>
    
    # Apache 2.4
    <IfModule mod_authz_core.c>
        Require all granted
    </IfModule>
    
    0 讨论(0)
提交回复
热议问题