使用 .htaccess 将 HTTP 重定向到 HTTPS

编程入门 行业动态 更新时间:2024-10-22 07:24:10
本文介绍了使用 .htaccess 将 HTTP 重定向到 HTTPS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

请不要向我推荐 长而非常详细的主题 超过 173 票.它对我不起作用.我也试过很多其他的(1、2, 3、4).他们都给我 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:

www.dukescasino/ - 完美运行

dukescasino/ - 重定向到上面很棒

dukescasino/ - redirects to the above which is great

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

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

www.dukescasino/

dukescasino/

这是当前的.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 提供
  • 搜索&过滤器
  • 滑块 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 .* %{HTTP_HOST}%{REQUEST_URI} [L,R=301] # Now, rewrite any request to the wrong domain to use www. RewriteCond %{HTTP_HOST} !^www. RewriteRule .* 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 ^.*$ %{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 ^.*$ %{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 (.*) %{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 ^(.*)$ www.%{HTTP_HOST}%{REQUEST_URI}$1 [R=301,L] RewriteCond %{HTTPS} off RewriteRule ^(.*)$ %{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 ^.*$ %{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

本文发布于:2023-10-31 15:52:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1546554.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重定向   htaccess   HTTP   HTTPS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!