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

mod_rewrite from http port XXXX to https port XXXY

· 52CE 我爱测

I can't get mod_rewrite work with the following Rules. RewriteCond "%{SERVER_NAME}" "home.myhome.net" RewriteCond "%{SERVER_PORT}" "^4388$" RewriteRule (.*) https://home.myhome.net:4389%{REQUEST_URI} [QSA,R=301,L] This Rule is se…

问题原文

原帖链接 · Thomas · CC BY-SA 3.0

I can't get mod_rewrite work with the following Rules.

RewriteCond "%{SERVER_NAME}" "home.myhome.net"
RewriteCond "%{SERVER_PORT}" "^4388$"
RewriteRule (.*) https://home.myhome.net:4389%{REQUEST_URI} [QSA,R=301,L]

This Rule is set on my http virtual host section. Problem is, that redirect only works the first time. The second time when I enter a differnt URL on http port 4388 I get redirected to https://home.myhome.net:4388/...

for example:

Any Idea how to fix that?

My OS details:

root@host:~# dpkg -l|grep apache
ii  apache2                                     2.4.18-2ubuntu3.5                            amd64        Apache HTTP Server
ii  apache2-bin                                 2.4.18-2ubuntu3.5                            amd64        Apache HTTP Server (modules and other binary files)
ii  apache2-data                                2.4.18-2ubuntu3.5                            all          Apache HTTP Server (common files)
ii  apache2-utils                               2.4.18-2ubuntu3.5                            amd64        Apache HTTP Server (utility programs for web servers)
ii  python-certbot-apache                       0.17.0-1+ubuntu16.04.1+certbot+1             all          Apache plugin for Certbot
root@host:~# lsb_release -d
Description:    Ubuntu 16.04.3 LTS

That's my Virtual Host Config, it is something different because I made some more tests, but same behavior. I also dropped for testing all other rewrite rules, with same result (and how I told in comments, for every test I flush my browser cache).

<VirtualHost *:80>
    ServerAdmin [email omitted]
    DocumentRoot /var/www/html

    <IfModule mod_rewrite.c>
        RewriteEngine on
        RewriteCond "%{SERVER_NAME}" "home.myhome.net"
        RewriteCond "%{SERVER_PORT}" "^4388$"
        RewriteRule ^ https://%{SERVER_NAME}:4389%{REQUEST_URI} [noescape,qsappend,redirect=301,last]
    </IfModule>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Oh, and I forgot to say, that this server is behind a NAT Gateway, which does a port forwarding from external port 4388 to internal host port 80 and also external port 4389 to internal port 443. Thats why I need to check for the port and hostname to match only external traffic.

EDIT: Okay, I have found something, but also don't have a solution: I compared the first and following browser request/response with the browser developer tools->network tab:

The first request give me the "301 Moved Permanently", but the following request gives me a "307 Internal Redirect".

So this seems a Server problem. Question is only, if it is a Bug in mod_rewrite or is it my fault, because the Rule is not okay.

EDIT: I tested now also with the current apache version 2.4.27-5.1+ubuntu16.04.1+deb.sury.org+1 from Ondřej Surý's Apache PPA

Regards, Thomas

已采纳解答

原帖链接 · Thomas · CC BY-SA 3.0

Ok, after long search I finally found the reason. I had set the HTTP Strict-Transport-Security (HSTS) on my https website, thats why the browser don't start again to open a http website on that url and force https protocol.

The given examples are all working.

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

资料来源与适用范围

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

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

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