使用.htaccess将http和https的所有非www重定向到www [英] Redirect all non-www to www for both http and https with .htaccess

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

问题描述

我想将所有非www重定向到www-如果请求是通过http进行的,则应重定向到 http://www .domain.com ,如果请求是通过https进行的,则直接发送到 https://www.domain.com

I want to redirect all non-www to www - if the request is through http it should redirect to http://www.domain.com, if the request is through https then direct to https://www.domain.com

我已经在.htaccess中尝试了以下操作,但是它将所有内容重定向到https

I have tried the following in my .htaccess, but it redirects everything to https

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


我已经使用了这段代码,而且解析得很完美.请检查是否正确.


I have used this code and it's resolving perfectly. Please check whether it's correct or not.

RewriteEngine On
RewriteBase /
#Redirect non-www to www

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

# Redirect to HTTPS
RewriteCond %{HTTPS} off
RewriteRule (.*) https://www.pnrstatusbuzz.in/%{REQUEST_URI}

推荐答案

您可以将此规则用作第一条规则:

RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

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

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