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

How can I serve all resources under HTTPS instead of HTTP without changing their URLs in the HTML code

· 52CE 我爱测

I know that by using an .htaccess file I can redirect everyone to https:// . However, my SSL certificate complains if all the content isn't served under HTTPS. Is there a way to convert all the resources to be served under HTTPS …

问题原文

原帖链接 · Jason · CC BY-SA 3.0

I know that by using an .htaccess file I can redirect everyone to https://. However, my SSL certificate complains if all the content isn't served under HTTPS.

Is there a way to convert all the resources to be served under HTTPS without changing all the http:// URLs to https:// in the HTML code?

EXAMPLE: If I type in http://domain.com it will go to https://domain.com. However, the images are not using HTTPS which causes issues. Is there a way to make ALL the resources use HTTPS?

I have this:

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www.domain\.com$ [NC]
RewriteRule ^ https://domain.com%{REQUEST_URI} [L,R=301]

However, my EV certificate will vanish because of unsecured content. So I need all the images to also be served under HTTPS.

已采纳解答

原帖链接 · Tom Brossman · CC BY-SA 3.0

You need to drop the 'http:' prefix from all image (and any other) links on your site. When you do so, your browser will default to whatever protocol the page loads with. For example this is best:

<a href="//www.example.com/">

And this is not:

<a href="http://www.example.com/">

The top example is perfectly valid markup and you will load all your resources over an HTTPS connection with the rewrite you already use. Start using it for all links as it works with both HTTP and HTTPS, so you won't ever have this issue again if you enable or disable SSL.

The only consideration is if you are using a CDN or other third-party hosted content on your site. If you are, the above advice will only work if they make their resources available over HTTPS also.

Once you do this, your EV (Extended validation) certificate will work as you are expecting and you should see all resources on the page load over a secure connection.

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

资料来源与适用范围

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

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

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