.htaccess 使用 SSL/HTTPS 将 www 重定向到非 www [英] .htaccess redirect www to non-www with SSL/HTTPS

查看:32
本文介绍了.htaccess 使用 SSL/HTTPS 将 www 重定向到非 www的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个域在一个 .htaccess 文件下运行,每个域都有一个 SSL 证书.

I've got several domains operating under a single .htaccess file, each having an SSL certificate.

我需要在每个域上强制使用 https 前缀,同时还要确保 www 版本重定向到 no-www 版本.

I need to force a https prefix on every domain while also ensuring www versions redirect to no-www ones.

下面是我的代码;它不起作用:

Below is my code; it doesn't work:

RewriteCond %{HTTP_HOST} ^www.%{HTTP_HOST}
RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI}/$1 [R=301,L]

我想要实现的是:将诸如 https://www.example.com 之类的内容重定向到 https://example.com.

What I want to achieve is to: redirect something like https://www.example.com to https://example.com.

我做错了什么,我该如何实现?

What am I doing wrong and how can I achieve it?

推荐答案

www 到非 www with https

RewriteEngine on

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

RewriteCond %{ENV:HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

这篇关于.htaccess 使用 SSL/HTTPS 将 www 重定向到非 www的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆