简体   繁体   English

htaccess重定向到HTTPS

[英]htaccess redirect for HTTPS

I'm having a bit of trouble with my htaccess file when trying to redirect http to https. 尝试将http重定向到https时,我的htaccess文件有点麻烦。 I've looked at other question on stack and i've tried to implement them to see if it will sort my problem but nothing has worked as of yet 我查看了堆栈上的其他问题,并尝试实现它们以查看它是否可以解决我的问题,但到目前为止还没有任何工作

I'm trying to do a redirect using htaccess to say that if its http:// then redirect to https:// 我正在尝试使用htaccess进行重定向,说如果它的http://则重定向到https://

Here is my original htaccess rule that i'm trying to modify for this to work with https 这是我最初的htaccess规则,我正在尝试对其进行修改以使其与https一起使用

# Get rid of www.
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

#route to index
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]

Can anyone shine a light on what I need to do for this to work, every attempt i've done seems to cause a server error 任何人都可以照亮我需要为此做的事情,我所做的每一次尝试似乎都会导致服务器错误

Regards 问候

Luke 卢克

You need to add an [OR] flag in your condition: 您需要在条件中添加一个[OR]标志:

# Get rid of www.
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.|)(.+)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

This makes it so you get redirected if it's not https or if there's a www 这样可以使您重定向到非https www站点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM