大网数据广告活动
服务器运维问答

Apache redirect http to https except specific source

· 52CE 我爱测

I want to be able to redirect all traffic from http to https on our apache config, except for if the incoming request is from a specific source (in this case, it's originating from the same IP as the server). Context: we have a Z…

问题原文

原帖链接 · Stephen Wright · CC BY-SA 4.0

I want to be able to redirect all traffic from http to https on our apache config, except for if the incoming request is from a specific source (in this case, it's originating from the same IP as the server).

Context: we have a Zend server running a php application with an apache config, and the same Zend server is also running a Job Queue which runs a http job to one of our REST endpoints. Obviously, we can't route this over https as it's internal traffic, but i'm not sure how to structure our RewriteConds so that it will correctly fall through and serve http only for that specific requester, but https for everyone else.

Here is the cond at the moment - i'm really not all that familiar with Apache syntax, but I've got to work with what I know ;)

<VirtualHost *:80>
    TimeOut 5600
    ServerName <name>
    ServerAlias <alias>
    RewriteEngine On
    RewriteCond %{HTTP_HOST} !=example\.com\:80
    RewriteCond %{HTTPS} !=on
    RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
    DocumentRoot <docroot>
    RewriteCond %{REQUEST_URI} !^(/index\.php|/favicon\.ico|/robots\.txt|/a/css(.*)|/a/js(.*)|/a/i(.*)|/alive\.html)
    RewriteRule ^(.*)$ /index.php/$1 [L]
    <Directory "<docroot>">
        Options All
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

<VirtualHost *:443>
    TimeOut 12000
    ServerName <name>
    ServerAlias <alias>
    DocumentRoot <docroot>
    RewriteEngine on
    RewriteCond %{REQUEST_URI} !^(/index\.php|/favicon\.ico|/robots\.txt|/a/css(.*)|/a/js(.*)|/a/i(.*)|/alive\.html)
    RewriteRule ^(.*)$ /index.php/$1 [L]
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
    <Directory "<docroot>">
        Options All
        AllowOverride All
        Require all granted
    </Directory>
    SSLEngine on
    ...ssl etc
</VirtualHost>

已采纳解答

原帖链接 · Esa Jokinen · CC BY-SA 4.0

The %{HTTP_HOST} refers to the server hostname i.e. the Host: header, while your desired condition is the specific source IP address. You should be comparing %{REMOTE_ADDR}, instead:

RewriteCond %{REMOTE_ADDR} !^127\.0\.0\.1

Also, you can use HTTPS for local traffic, but it's not necessary.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-03-16)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。