使用 .htaccess 将 HTTP 重定向到 HTTPS [英] Use .htaccess to redirect HTTP to HTTPs

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

问题描述

请不要向我推荐 长而非常详细的主题 超过 173 票.它对我不起作用.我也试过很多其他的(12, 34).他们都给我 TOO_MANY_REDIRECTS 或错误 500.所以,这是我的问题:

Please, don't recommend me the long and very detailed thread with more than 173 upvotes. It didn't work for me. I have also tried many others (1, 2, 3, 4). They all give me TOO_MANY_REDIRECTS or error 500. So, here is my issue:

使用我当前的 .htaccess,会发生以下情况:

With my current .htaccess, this is what happens:

https://www.dukescasino.com/ - 完美运行

https://dukescasino.com/ - 重定向到上面很棒

https://dukescasino.com/ - redirects to the above which is great

下面的两个选项加载正常,但它应该重定向到 https 版本:

The two options below loads fine, but it should be redirecting to the https version:

http://www.dukescasino.com/

http://dukescasino.com/

这是当前的.htaccess:

Here is the current .htaccess:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

# BEGIN WordPress
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

我不认为它是相关的,但如果是,这里是当前活动插件的列表:

I don't believe it is relevant, but if so, here is the list of current active plugins:

  • 高级自定义字段
  • 一站式 SEO 包
  • 导航菜单的 Bop 搜索框项目类型
  • 联系表格 7
  • 禁用评论
  • Google XML 站点地图
  • Jetpack 由 WordPress.com 提供
  • 搜索&过滤器
  • 滑块 WD
  • TablePress
  • UpdraftPlus - 备份/恢复
  • Wordfence 安全性
  • WPide
  • WP Smush
  • WP 超级缓存

编辑 1 - 执行的测试:

测试 A:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{HTTPS} off
# First rewrite to HTTPS:
# Don't put www. here. If it is already there it will be included, if not
# the subsequent rule will catch it.
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Now, rewrite any request to the wrong domain to use www.
RewriteCond %{HTTP_HOST} !^www.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# BEGIN WordPress
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

结果: ERR_TOO_MANY_REDIRECTS

Result: ERR_TOO_MANY_REDIRECTS

测试 B:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

# BEGIN WordPress
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

结果: ERR_TOO_MANY_REDIRECTS

Result: ERR_TOO_MANY_REDIRECTS

测试 C:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{SERVER_PORT} ^80$
 RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

# BEGIN WordPress
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

结果: ERR_TOO_MANY_REDIRECTS

Result: ERR_TOO_MANY_REDIRECTS

测试 D:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

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

# BEGIN WordPress
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

结果: ERR_TOO_MANY_REDIRECTS

Result: ERR_TOO_MANY_REDIRECTS

测试 E:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

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

# BEGIN WordPress
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

结果: 302 找到.此外,在尝试使用 ErrorDocument 处理请求时遇到 500 Internal Server Error 错误.

Result: 302 found. Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.

推荐答案

问题解决!

最终的.htaccess:

Final .htaccess:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

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

# BEGIN WordPress
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

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

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