使用htaccess的Let's Encrypt

18

这是我当前的 /frontend/web 的 htaccess 配置

RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME} [R,L]
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php

我试图插入这个:

RewriteCond %{REQUEST_URI} !^.well-known/acme-challenge/$
或者
RewriteCond %{REQUEST_URI} ! /\.well-known|^\.well-known

以上

RewriteRule ^.*$ https://%{SERVER_NAME} [R,L]
创建 Let's Encrypt 证书的方法不起作用。以下是用于创建证书的 Let's Encrypt 命令(由于使用 Centos6,启用了调试模式):
./letsencrypt-auto --debug certonly --webroot -w /var/www/html/example.com/frontend/web/ --email example@gmail.com --domains example.com

letsencrypt错误:

The following errors were reported by the server:

Domain: example.com
Type:   unauthorized
Detail: Invalid response from
http://example.com/.well-known/acme-challenge/%acme%

点击上面的链接会跳转到网站协议的 HTTPS 版本。如果我删掉重定向到 HTTPS 的操作,我会收到一个证书成功接收的消息。结论是:.well-known 仍然会被发送到 HTTPS,我的设置没有生效,我做错了什么?


只需将此条件添加到.htaccess文件中的RewriteEngine On之后即可:RewriteCond %{HTTP_HOST} !^\.well-known/.+ - ETech
6个回答

25

最干净的方法,而无需更改任何规则,就是添加一条单独的规则,在所有其他规则之前,有效地禁用目录中文件的重写,例如:

RewriteRule ^\.well-known/.+ - [END]

你可能希望在规则之前立即添加文件存在性检查,这样就会显示你的自定义错误响应页面,而不是服务器的默认页面:

RewriteCond %{REQUEST_FILENAME} -f

2
RewriteRule ^.well-known/.+ - [END] 是唯一对我有效的选项,我尝试了很多其他方法! - Aleksandar Pavić
3
在没有文档根目录的Apache 2.4虚拟主机上,我必须在“^”后面添加一个斜杠:RewriteRule ^/\.well-known/.+ - [END] - cweiske

8

最终我使用了这个配置,在cakephp 2上运作良好:

将以下内容放置在.htaccess文件中,该文件位于您的Web根目录和应用程序文件夹之上,与您的应用程序位于同一文件夹中。

<IfModule mod_rewrite.c>    
  RewriteEngine on

  RewriteRule ^.well-known/ - [L,NC]

  RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
  RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

  RewriteCond %{HTTPS} !=on
  RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

  RewriteRule    ^$ app/webroot/    [L]
  RewriteRule    (.*) app/webroot/$1 [L]
</IfModule>

只需更改底部两行以适应您的系统即可。


谢谢!这刚刚替换了我的 .htaccess 文件,而且长度缩短了10倍。 - laka

3
这个对我有用:

这个对我有用:

RewriteCond %{REQUEST_URI} !^/.well-known/(.*)

2
这正是我需要的框架,它可以阻止访问公共文件夹以外的文件夹。 - MasterT
1
这是对我有效的解决方案。无论您现有的配置如何,这都是一个简单的规则来排除.well-known请求。谢谢! - gillytech

1
我通常会在我的虚拟主机配置中添加一个别名,指向一个不安全的环境。通常情况下,我的开发或者测试服务器是受到htaccess保护的,而生产系统(显然)没有。
Apache 虚拟主机配置:

protected.example.com.conf

<VirtualHost *:80>
    Alias /.well-known /var/www/example.com/.well-known
    <Directory /var/www/example.com/.well-known>
        Require all granted
    </Directory>
</VirtualHost>

当然,你还需要调整你的letsencrypt命令,它应该指向别名目标。
./letsencrypt-auto --debug certonly --webroot -w /var/www/example.com/.well-known --email example@gmail.com --domains example.com

1
将其放在.htaccess文件中,如下所示:

RewriteRule "^.well-known/acme-challenge" - [L]

1
首先,您在点号之前缺少转义字符。如果未转义,则点号匹配任何字符。此外,您应该检查mod_rewrite.c,而且您没有提到,您的代码需要放在任何其他重写规则之前的顶部。 - Emanuel S.

0
RewriteEngine On

RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/\.well-known/
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接