问题原文
原帖链接 · 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-knownexceptions 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:
- Apache htaccess multiple rewrite conditions for multiple rules
- Explanation of new line in htaccess file: RewriteCond %{REQUEST_URI} !^/.well-known/pki-validation/(?:\ Ballot169)?
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
