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

Apache Conditional Redirect HTTP to HTTPS

· 52CE 我爱测

all. I have a syntax problem. I've tried following other sources, but It's just not working. I've successfully created an unconditional HTTP to HTTPS redirect. Go to http://www.robjvargas.com , wind up at HTTPS. All well and good…

问题原文

原帖链接 · Rob Vargas · CC BY-SA 3.0

all. I have a syntax problem. I've tried following other sources, but It's just not working.

I've successfully created an unconditional HTTP to HTTPS redirect. Go to http://www.robjvargas.com, wind up at HTTPS. All well and good there.

But now my certificate seems to need HTTP. It's using Let's Encrypt, and so needs renewal every 90 days. With the redirect in effect, a dry-run fails. With the redirect off, the dry run succeeds.

So I tried to put a RewriteCond in to exempt the cert renewal traffic:

# HTTP to HTTPS redirect
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{Request_URI} !^./acme-challenge/*
RewriteRule (.*) https://www.robjvargas.com$1 [R,L]

Only it doesn't work. I think I'm missing something worthy of a forehead slap once I see it, but I'm not seeing it. With the code above, the HTTP to HTTPS redirect still seems universal. So, clearly, the RewriteCond is incorrect. The period before the URI is probably wrong, but I've tried both with and without that.

I also put the full URI into the RewriteCond, but that didn't exempt it, either.

I'm close. I don't get any errors. It just never meets the intended condition.

What am I missing?

已采纳解答

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

But now my certificate seems to need HTTP.

This is not quite correct. I am personally renewing over https. In order for that to work however, you need make sure to allow access to the challenge URI in your https VirtualHost by copying the relevant part from the http VirtualHost. If you can't or don't want to do that, here's how to make it work over http with the conditional redirect you started to write.

The problem is that your current regex would match the challenge uri. According to the Apache Docs the REQUEST_URI starts with the / so you should look for the challenge uri like this: ^\/\.well-known\/.*$. This will match the challenge URI (http://example.com/.well-known/acme-challenge).

All put together:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^\/\.well-known\/.*$
RewriteRule (.*) https://www.robjvargas.com/$1 [R,L]

In case of future troubles, use this: htaccess tester

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

资料来源与适用范围

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

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

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