【问题标题】:.htaccess comparing with API_VERSION.htaccess 与 API_VERSION 比较
【发布时间】:2013-07-26 14:18:44
【问题描述】:

我正在为一个 htaccess 文件而苦苦挣扎。

我需要做的是,当apache版本低于某个版本时,做一个重写规则,否则一个不同的规则。

这是关于 apache 2.2.7 中引入的反向引用,但我仍然需要支持旧的 apache 版本。

我想做的是这样的:

<IfModule mod_rewrite.c>
    RewriteEngine On

    if API_VERSION <= 2.2.6(or 20051115:5) then
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
    else
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^(.*)$ index.php?url=$1 [QSA,L,B]
    endif
</IfModule>

如果可用,我需要“B”,有人知道这是否可以工作或以不同的方式获得相同的结果?

【问题讨论】:

    标签: apache .htaccess backreference


    【解决方案1】:

    你可以试试这个:

    <IfModule mod_rewrite.c>
    RewriteEngine On
    
    # Lexographic string match
    RewriteCond %{API_VERSION} <=20051115:5
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
    
    # Lexographic string match
    RewriteCond %{API_VERSION} >20051115:5
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?url=$1 [QSA,L,B]
    </IfModule>
    

    或安装mod_version 并尝试:

    <IfVersion <= 2.2.6>
        <IfModule mod_rewrite.c>
        RewriteEngine On
    
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
        </IfModule>
    </IfVersion>
    <IfVersion > 2.2.6 >
        <IfModule mod_rewrite.c>
        RewriteEngine On
    
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^(.*)$ index.php?url=$1 [QSA,L,B]
        </IfModule>
    </IfVersion>
    

    【讨论】:

    • 会不会即使条件不匹配也不能放',B'。它给了我一个 500 错误
    • @Crazy 很可能,我已经用替代解决方案更新了我的答案
    • Sweet!,就像一个魅力。 mod_version 已在我的 centos 5(2.2.3) 和 6(2.2.15) apache 安装中预安装并启用。
    猜你喜欢
    • 2011-09-15
    • 2021-07-14
    • 2016-10-22
    • 1970-01-01
    • 2015-05-25
    • 2014-04-23
    • 2011-03-27
    • 1970-01-01
    • 2016-05-14
    相关资源
    最近更新 更多