【问题标题】:Automatic redirects from http to https自动从 http 重定向到 https
【发布时间】:2019-02-12 01:02:38
【问题描述】:

我使用MVC模式,我的路由如下:

http://Myurl.com/class/method/param1/param2/...

通常一切都很好但是当我使用下面的代码强制使用https时,我的路由不起作用

RewriteCond %{HTTPS} off

.ht 访问代码:

DirectoryIndex index.php
Options -Indexes


<Files '.htaccess' >
  Deny from all
</Files>


RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !\.(jpg|png|css|js|eot|svg|ttf|woff|apk)$
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST} [L]

【问题讨论】:

  • RewriteCond %{HTTPS} off 表示“仅在 HTTPS 关闭时执行此规则”。由于您打开了 HTTPS,它不再做任何事情。删除条件。 (旁注:您发布的代码不会强制使用 HTTPS。)
  • 感谢您回答我的问题。我编辑了 RewriteRule 但没有再次工作:(
  • 有两个原因:a) 您需要一个 [R] 标志来重定向,b) 您现在已经删除了站点所依赖的旧 index.php 路由规则。
  • 我使用 DirectoryIndex index.php ,所以 %{REQUEST_URI} 应该自动调用 index.php 。错了吗?
  • DirectoryIndex 将只处理对/ 的请求。

标签: php .htaccess redirect https


【解决方案1】:

您需要重定向。这应该适合你。将其粘贴到您的根目录 .htaccess

RewriteEngine On 
RewriteCond %{HTTP_HOST} !^example\.com 
RewriteRule (.*) https://example.com/$1 [R=301,L]

【讨论】:

  • 我不认为RewriteCond 指的是正确的情况。我使用了RewriteCond %{SCRIPT_URI} ^http.*,但我的路由不起作用。路由发生在index.php 文件中
猜你喜欢
  • 2017-03-17
  • 2019-05-15
  • 1970-01-01
  • 2021-09-10
  • 2019-11-01
  • 2019-09-10
  • 2017-10-06
  • 1970-01-01
  • 2017-11-04
相关资源
最近更新 更多