Php .htaccess强制访问文件夹中的HTTPS

Php .htaccess强制访问文件夹中的HTTPS,php,.htaccess,https,Php,.htaccess,Https,我正在尝试重定向论坛索引,以便在所有内容中使用HTTPS,但是.htaccess对我来说是一个全新的东西,我正在学习,因此任何人都可以帮忙,我将不胜感激:) 所以我把论坛放在一个叫做SMF的子目录中。目前所有内容都将访问www.website.net/smf/index.php 但我想将所有内容重定向到HTTPS,同时保留/smf/index.php结构 以下是我在主html_public中的当前.htaccess: # .htaccess main domain to subdirectory

我正在尝试重定向论坛索引,以便在所有内容中使用HTTPS,但是.htaccess对我来说是一个全新的东西,我正在学习,因此任何人都可以帮忙,我将不胜感激:)

所以我把论坛放在一个叫做SMF的子目录中。目前所有内容都将访问www.website.net/smf/index.php

但我想将所有内容重定向到HTTPS,同时保留/smf/index.php结构

以下是我在主html_public中的当前.htaccess:

# .htaccess main domain to subdirectory redirect
# Do not change this line.
RewriteEngine on
# Change example.com to be your main domain.
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
# Change 'subdirectory' to be the directory you will use for your main domain.
RewriteCond %{REQUEST_URI} !^/subdirectory/
# Don't change the following two lines.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Change 'subdirectory' to be the directory you will use for your main domain.
RewriteRule ^(.*)$ /subdirectory/$1
# Change example.com to be your main domain again.
# Change 'subdirectory' to be the directory you will use for your main domain
# followed by / then the main file for your site, index.php, index.html, etc.
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ subdirectory/index.php [L] 
当我改变任何事情时,一切都变得疯狂。论坛中的所有内容都在子目录文件夹中,包括index.php等等。(请注意,我在复制粘贴代码中将网站url改为example.com,子目录改为subdirectory)

我正在尝试让网站显示https,但每当我尝试一些东西时,它都不起作用,给了我,我无法删除//:(

我是否可以清理代码,使其更简单、更容易理解


谢谢!

我已经为.htaccess文件苦苦挣扎了好几个晚上……但这里的交易是,这个文件实际上与php无关,但实际上与Apache相关。这是一个独立于php的程序(它本身就是一种编程语言和编译器)。这并不意味着你可以在没有浏览器的情况下在终端上运行php。有什么问题吗,因为Apache不是用php编程的,它是专门为处理HTTP而设计的服务器请求一些代码以使其工作可能与操作系统有关。这是在本地运行并尝试部署到远程服务器时遇到很多困难的原因。这些都是我的经验,因此如果有人知道更多,请随时与我分享

这就是我现在用来发送到https的内容

#删除www。
重写基/
重写条件%{HTTP_HOST}^www\.(.*)$[NC]
重写规则^(.*)$https://%1/$1[R=301,L]

但我以前也用过这些变体

#打开https(Cent OS)
重写条件%{HTTPS}关闭
重写规则^(.*)$https://%{HTTP\u HOST}%{REQUEST\u URI}[L,R=301]

#打开https(Mac OS)
RewriteCond%{HTTP:X-Forwarded-Proto}!https
重写规则^(.*)$https://%{HTTP_HOST}%{REQUEST_URI}[L,R=301]

为了便于移植,我建议您自己直接在php中实现此功能

您可以使用下面类似的方法检测https,这将设置一个名为“https”的全局常量布尔值

\define('HTTPS',($服务器['HTTP\u X\u转发的\u协议]]为false)=='HTTPS'| |(isset($服务器['HTTPS'])和&$服务器['HTTPS'!='off');

这是PHP7代码

如果它是错误的,那么你只需重定向你认为合适的方式

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=307,L,NC]

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_FILENAME}.php [L]

RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_FILENAME}.html [L]

RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

#ErrorDocument 404 /404.php
#ErrorDocument 403 /403.php

Options -Indexes