大网数据广告活动
服务器运维问答

Redirecting from http to https in Apache

· 52CE 我爱测

This is a canonical question about redirecting from http to https in Apache Related: Everything You Ever Wanted to Know About Mod_Rewrite Rules but Were Afraid to Ask I have an Appache web server which serves both http://example.…

问题原文

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

This is a canonical question about redirecting from http to https in Apache

Related:

I have an Appache web server which serves both http://example.com/ and https://example.com/. I want to redirect all http requests to https. Currently I'm using this .htaccess rule to redirect http requests to https.

RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} 

It's working as expected for example.com but the same rule is not working for sub links, when I access existing links, like example.com/about it will still load in http, no redirection happens for existing links.

How can I make Apache redirect all http requests to https?

已采纳解答

原帖链接 · Vinícius Ferrão · CC BY-SA 4.0

You should configure Apache Virtualhosts to do the job. RewriteMod isn't the appropriate solution for this case and .htaccess isn't either.

In your httpd.conf or equivalent use the following lines accordingly your needs. Edit it to your domain and site.

<VirtualHost *:80>
   ServerName www.example.com example.com
   Redirect permanent / https://example.com/
</VirtualHost>

<VirtualHost _default_:443>
   ServerName example.com
   DocumentRoot /usr/local/www/apache2/htdocs
   SSLEngine On

   ** Additional configurations here **

</VirtualHost>

Hope this clarifies the procedure.

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

资料来源与适用范围

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

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

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