大网数据广告活动
网站运营与故障问答

Could someone explain what these .htaccess rules and ".well-known" conditions are for?

· 52CE 我爱测

I am confused about both the purpose of .well-known exceptions and so many conditions. Is this just something temporarily put in place by the host or is it best practice to keep it in place? Why are there two sets of conditions a…

问题原文

原帖链接 · Michael Moriarty · CC BY-SA 4.0

I am confused about both the purpose of .well-known exceptions and so many conditions. Is this just something temporarily put in place by the host or is it best practice to keep it in place? Why are there two sets of conditions and rules. Can they be combined into one set?

RewriteEngine On
RewriteCond %{SERVER_PORT} 80 
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/(?:\Ballot169)?
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

RewriteCond %{REQUEST_METHOD} =POST
RewriteCond %{HTTP_USER_AGENT} ^.*(opera|mozilla|firefox|msie|safari).*$ [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.+/trackback/?\ HTTP/ [NC]
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/(?:\ Ballot169)?
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule .? - [F,NS,L]

已采纳解答

原帖链接 · MrWhite · CC BY-SA 4.0

Why are there two sets of conditions and rules. Can they be combined into one set?

These are two rules that serve two different purposes - they cannot be combined. Taking out the (unnecessary) ".well-known" conditions, we have:

RewriteEngine On
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

RewriteCond %{REQUEST_METHOD} =POST
RewriteCond %{HTTP_USER_AGENT} ^.*(opera|mozilla|firefox|msie|safari).*$ [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.+/trackback/?\ HTTP/ [NC]
RewriteRule .? - [F,NS,L]

The first is an HTTP to HTTPS redirect. (Which should really be a 301 permanent redirect, not a 302 temporary redirect as it is currently written.)

The second rule would seem to block Trackbacks. This is a "feature" that allows websites/blogs (particularly WordPress) to acknowledge linkbacks to articles and is often shown as a comment. However, this can be abused by spammers, hence the desire to block them.

I am confused about both the purpose of .well-known exceptions and so many conditions. Is this just something temporary put in place by the host or is it best practice to keep it in place?

These conditions play no part in the rules themselves, they are simply creating exceptions so the rules don't run when these requests are made. This occurs when your host auto-renews security certificates.

They are only needed temporarily (if at all). However, they are likely to be required again in a few (3?) months time when the certificates are next renewed.

However, as noted by @Stephen in comments, for these particular rules, the conditions are most probably superfluous anyway. However, cPanel does not discriminate, it blindly adds these conditions before every RewriteRule directive. If you manually remove them then cPanel is likely to add them back when it comes to cert renewal (as mentioned).

See also my answers on the following ServerFault questions for more information regarding these conditions:

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

资料来源与适用范围

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

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

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