如何使用 htaccess 强制执行 HTTPS 和 PHP MVC?

问题描述 投票:0回答:2

这是PHP MVC

的htaccess代码


RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^(.*)$ index.php?url=$1 [L]

我想要的是:

  • 强制https
  • 然后转到 index.php 文件并发送 url 数据

我像这样添加了新的 htaccess 文件



RewriteEngine On

RewriteCond %{HTTP:X-Forwarded-Proto} !https

RewriteCond %{HTTPS} off

RewriteCond %{HTTP:CF-Visitor} !{"scheme":"https"}

RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

在父目录中

但是,它仍然没有强制 HTTPS

请回答

对不起我的英语不好

php apache .htaccess model-view-controller
2个回答
0
投票
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

0
投票

谢谢大家, 我找到了答案,我认为这不是 100% 准确。 所以如果我错了请纠正我:)

只需在 .htaccess 文件上执行此操作

#Turn On RewriteEngine
RewriteEngine On
#If the HTTPS is off
RewriteCond %{HTTPS} off
#Redirect to HTTPS
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#%{HTTP_HOST} is equal to the host domain 

#if the request url is a file ignore it
RewriteCond %{REQUEST_FILENAME} !-d
#if the request url is a dir, ignore it
RewriteCond %{REQUEST_FILENAME} !-f
#whatever was typed from start to end, move to index.php and send whatever the user has typed. and dont apply any RewriteRule after this
RewriteRule ^(.*)$ index.php?url=$1 [END]
  • 强制 HTTPS ✓
  • 转到索引,并发送 url 数据 ✓

有关 RewriteEngine 的信息来源是Here

这很奇怪,我回答我自己的问题。

如果我的解释难以理解,我很抱歉:)

© www.soinside.com 2019 - 2024. All rights reserved.