.htaccess排除URI

时间:2016-04-26 19:45:42

标签: .htaccess mod-rewrite

有工作htaccess,阻止特定国家和/或语言,

RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^NL$ [NC,OR]
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^BY$ [NC,OR]
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^UA$ [NC,OR]
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^RU$ [NC,OR]
RewriteCond %{HTTP:Accept-Language} (nl) [NC,OR]
RewriteCond %{HTTP:Accept-Language} (be) [NC,OR]
RewriteCond %{HTTP:Accept-Language} (uk) [NC,OR]
RewriteCond %{HTTP:Accept-Language} (ru) [NC]
RewriteRule ^(.*)$ cap.html [L]

但我需要访问特定路线,例如api.html到任何国家/地区/语言。 我尝试添加异常

RewriteCond %{REQUEST_URI} !^/api\.html 

但它不起作用,我已经尝试了数百个条件但放弃了6个小时,请告诉我我做错了什么。

    RewriteEngine On

    # Exclude countries, forbidden by law
    RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^NL$ [NC,OR]
    RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^BY$ [NC,OR]
    RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^UA$ [NC,OR]
    RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^RU$ [NC,OR]
    RewriteCond %{HTTP:Accept-Language} (nl) [NC,OR]
    RewriteCond %{HTTP:Accept-Language} (be) [NC,OR]
    RewriteCond %{HTTP:Accept-Language} (uk) [NC,OR]
    RewriteCond %{HTTP:Accept-Language} (ru) [NC]
    RewriteCond %{REQUEST_URI} !^/api\.html
    RewriteRule ^(.*)$ cap.html [L]

    RewriteCond %{REQUEST_FILENAME} -s [OR]
    RewriteCond %{REQUEST_FILENAME} -l [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^.*$ - [L]
    RewriteRule ^.*$ index.php [L]

3 个答案:

答案 0 :(得分:0)

尝试:

RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^NL$ [NC,OR]
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^BY$ [NC,OR]
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^UA$ [NC,OR]
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^RU$ [NC,OR]
RewriteCond %{HTTP:Accept-Language} (nl) [NC,OR]
RewriteCond %{HTTP:Accept-Language} (be) [NC,OR]
RewriteCond %{HTTP:Accept-Language} (uk) [NC,OR]
RewriteCond %{HTTP:Accept-Language} (ru) [NC]
#rewrite everything to "cap.html" except "api.html"
RewriteRule ^((?!api\.html).*)$ cap.html [L]

答案 1 :(得分:0)

您当前的代码似乎合乎逻辑,但您可以尝试将条件合并在一起,并将最后一个条件移到顶部:

RewriteCond %{REQUEST_URI} !^/api\.html [NC]
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^(?:NL|BY|UA|RU)$ [NC,OR]
RewriteCond %{HTTP:Accept-Language} (?:nl|be|uk|ru) [NC]
RewriteRule ^ cap.html [L]

答案 2 :(得分:0)

我以稍微不同的方式解决了问题 刚刚创建了api.php文件并添加了跳过现有文件的规则

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^(?:NL|BY|UA|RU)$ [NC,OR]
RewriteCond %{HTTP:Accept-Language} (?:nl|be|uk|ru) [NC]
RewriteRule ^ cap.html [L]

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [L]
RewriteRule ^.*$ index.php [L]