【问题标题】:get country code from http header and add it to url parameters in apache从 http 标头获取国家代码并将其添加到 apache 中的 url 参数中
【发布时间】:2017-11-13 10:43:59
【问题描述】:

我正在使用 cloudflare 向服务器提供 http 标头 HTTP_CF_IPCOUNTRY 中的国家代码。

我需要从 http 标头获取国家代码并使用 .conf 文件(虚拟主机)将其添加到 apache 服务器中的 url 参数中。这可能吗?

服务器版本:Apache/2.4.7 (Ubuntu)

我有一个角度 dist 文件夹,我正在使用 apache 服务器提供文件,并且需要 url 参数中的国家/地区。

我的 php 服务和 Angular 应用程序的配置文件(/consultation/*)

<VirtualHost *:80>
    ServerName hero.azure
    ServerAlias hero.com
    ErrorLog /var/log/apache2/error.log
    DocumentRoot /var/www/hero/public
    SetEnv APPLICATION_ENV "testing"
    AliasMatch ^/consultation(.*) "/var/www/ng2/dist/index.html"
    Alias "/ng2" "/var/www/ng2/dist/ng2/"
    <Directory /var/www/hero/public>
     <LimitExcept OPTIONS>
            AuthType Basic
            AuthName "user"
            AuthUserFile /etc/apache2/htpass
            Require user user
    </LimitExcept>
            DirectoryIndex index.php
            AllowOverride All
            Order allow,deny
            Allow from all
    </Directory>
</VirtualHost>

这里有一些尝试添加查询

RewriteEngine on
RewriteCond %{QUERY_STRING} country
RewriteRule ^/consultation(.*)$ ^/consultation/(.*)?country="%{CF-IPCOUNTRY}i"&%{QUERY_STRING} [QSA]

【问题讨论】:

标签: angular apache apache2.4


【解决方案1】:

通过将这条规则添加到虚拟主机来解决:

SetEnvIf CF-IPCountry "(.*)" COUNTRY=$1
<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{ENV:COUNTRY} [A-Z]{2}
    RewriteCond %{QUERY_STRING} !country [NC]
    RewriteRule ^/consultation(.*) %{REQUEST_URI}?country=%{ENV:COUNTRY} [L,QSA,R]
</IfModule>

【讨论】:

    猜你喜欢
    • 2016-08-14
    • 2021-02-17
    • 2016-06-11
    • 1970-01-01
    • 1970-01-01
    • 2016-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多