问题原文
原帖链接 · jxmot_wd · CC BY-SA 3.0
I'm currently using my .htaccess to force all HTTP requests to HTTPS, and that is working very well. However I would also like to redirect some TLDs. For example -
http(s)://(www.)example.info -> https://example.com or https://www.example.com
http(s)://(www.)example.net -> https://example.com or https://www.example.com
My current .htaccess file contains:
RewriteOptions inherit
RewriteEngine on
RewriteCond %{HTTPS} !on
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
The incoming requests that I would like to redirect from may or may not be HTTPS, and the "www" may or may not be present.
The other thing I'd like to do, and sorry if this should be in a separate question but I believe that the answers would be related....
Can I wrap the previously described redirects in a conditional that will skip them if the server is localhost?
已采纳解答
原帖链接 · MrWhite · CC BY-SA 3.0
http(s)://(www.)example.info->https://example.comorhttps://www.example.com
On reading your question it doesn't sound like you want to canonicalise the www subdomain? However, you should be. Decide whether it should be www.example.com or example.com and stick to that. For the rest of this answer, I'll assume www.example.com should be the canonical domain.
The easiest way would be to say... if the request is not for the canonical domain then redirect to the canonical domain. This will then catch any of the other TLDs and whether they are www or not (and even whether they are FQ or not).
For example:
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !=www.example.com
RewriteRule .* https://www.example.com%{REQUEST_URI} [R=301,L]
Notes about the above:
- Addition of the
ORflag on the first condition. So either of the first two conditions must be successful (not just both of them). - The
=prefix defines a lexicographical exact string comparison. ie. not a regex. So no need to escape literal dots or use start/end-of-string anchors. - Removal of the parentheses on the
RewriteRulepattern. No need to capture anything, since you aren't using it. - Added the flags on the
RewriteRule. This should ultimately be a 301 permanent redirect, although first testing with a 302 temporary redirect (to avoid caching) is preferable. If you ever add more directives then you will need theLflag to stop further processing. - Add back the two cPanel additions if you wish (required for AUTO-SSL renewal). Or these can be separated into their own rule.
Can I wrap the previously described redirects in a conditional that will skip them if the server is
localhost?
There are quite a few ways to block the directives from local development (which can depend on your server config / version). If you simply want to block this single mod_rewrite rule from applying to the Host localhost then just add another condition:
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !=www.example.com
RewriteCond %{HTTP_HOST} !=localhost
RewriteRule .* https://www.example.com%{REQUEST_URI} [R=301,L]
ie. Only when the Host header is not "localhost".
Summary
So, in summary, adding back the cPanel conditions (for AUTO-SSL renewal), we have:
RewriteOptions inherit
RewriteEngine on
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !=www.example.com
RewriteCond %{HTTP_HOST} !=localhost
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule .* https://www.example.com%{REQUEST_URI} [R=301,L]
However, also note the comment by @xsrf if you are planning on implementing HSTS.
See also my answer to the following ServerFault question for more information regarding those additional directives (AUTO-SSL renewal) added by cPanel:
https://serverfault.com/questions/884915/apache-htaccess-multiple-rewrite-conditions-for-multiple-rules
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
