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

How is it best to redirect http to https in cPanel?

· 52CE 我爱测

I am setting up a new site on a domain that has an SSL certificate. The DNS is all configured correctly and I can access the site on either http://example.com or https://example.com URLs. Currently, it is redirecting http://examp…

问题原文

原帖链接 · Alan Spark · CC BY-SA 4.0

I am setting up a new site on a domain that has an SSL certificate. The DNS is all configured correctly and I can access the site on either http://example.com or https://example.com URLs.

Currently, it is redirecting http://example.com to https://example.com and http://www.example.com to https://example.com.

However, I don't really understand how that is working because it works even if I delete the .htaccess file. If I do change the .htaccess to redirect using any of the techniques that I've come across then I get a "too many redirects" error. There must be something else going on that I don't understand.

Ideally, I'd like it to redirect http://example.com/anything to https://example.com/anything

Any ideas?

My server is using CentOS 6.6. Output from "uname -r":

2.6.32-504.12.2.el6.x86_64

Can provide further information if needed.

Added example of .htaccess code that caused "too many redirects":

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

已采纳解答

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

It's quite probable that your web application (ie. WordPress - mentioned in comments) is already issuing the appropriate HTTP to HTTPS and www to non-www redirects. However, using .htaccess is arguably more efficient *1, and your web application may not be issuing the correct redirect(s) if you are planning on implementing HSTS.

(*1 - Although using your main server config would be preferable.)


Added example of .htaccess code that caused "too many redirects":

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

This isn't a HTTP to HTTPS redirect (in fact, it isn't anything) and will certainly result is a redirect loop (ie. "too many redirects" browser error). It basically says, if the requested host is example.com then redirect to example.com (irrespective of protocol). So, it just redirects to itself.

As mentioned in comments, you can't implement HTTP to HTTPS redirects using the cPanel "redirect" interface. You can only perform very simple redirects using the cPanel interface, which is far from perfect and not without its caveats. And if you are using WordPress then you can't even configure simple redirects using cPanel with some manual editing of the .htaccess file. (cPanel always adds the redirect code at the end of the .htaccess file - which is notably after the WordPress front-controller. So in most cases the redirect directives are never processed anyway. The cPanel documentation notifies you of this.)

Any redirect directives you add (using mod_rewrite) to your .htaccess file, must go before the WordPress front-controller, ie. at the top of your .htaccess file.

If you are planning on implementing HSTS then the HTTP to HTTPS redirect must redirect to the same host, so the HTTP to HTTPS should go before (and be separate from) the canonical www/non-www redirect. See @jcanepa's answer for the appropriate HTTP to HTTPS redirect in this scenario. You can then follow this with a www to non-www redirect (which appears to be your canonical hostname). For example:

# www to non-www redirect only
# (NB: This must go AFTER the HTTP to HTTPS redirect if implementing HSTS)
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule .* https://example.com%{REQUEST_URI} [R=301,L]

If, however, you have no intention of implementing HSTS then you can combine the HTTP to HTTPS and www to non-www canonical redirect in a single redirect instead (which prevents a potential second redirect). For example:

RewriteEngine On

# HTTP to HTTPS and www to non-www
# (NB: Not suitable for HSTS)
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule .* https://example.com%{REQUEST_URI} [R=301,L]

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

资料来源与适用范围

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

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

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