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

.htaccess - how do i redirect TLDs?

· 52CE 我爱测

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://ww…

问题原文

原帖链接 · 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.com or https://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 OR flag 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 RewriteRule pattern. 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 the L flag 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

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

资料来源与适用范围

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

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

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