首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >强制.htaccess到文件夹中的HTTPS

强制.htaccess到文件夹中的HTTPS
EN

Stack Overflow用户
提问于 2018-08-07 08:26:15
回答 2查看 82关注 0票数 0

我正在尝试重定向一个论坛索引,以便使用HTTPS做任何事情,但.htaccess对我来说是非常新的,我正在学习,所以任何人都可以帮我一把,我会很感激的:)

因此,我在一个名为SMF的子目录中有一个论坛。目前一切都转到www.website.net/smf/index.php

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

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

代码语言:javascript
运行
复制
# .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,并将子目录更改为子目录)

我试着让网站显示https,我都快疯了,但是每当我尝试一些东西时,它都不起作用,给我https://www.website.com//smf/index.php,我不能删除// :(

有没有什么办法我可以清理代码,让它更简单,更容易理解?

谢谢!

EN

回答 2

Stack Overflow用户

发布于 2018-08-07 10:20:38

我已经为.htaccess文件纠结了很多个晚上……但事情是这样的,这个文件实际上与php无关,但实际上与Apache相关。这是一个独立于PHP的程序(PHP本身是一种编程语言和编译器)。我不想离题太远,但这意味着你可以在没有浏览器的情况下在终端上运行php。因为Apache不是用PHP编写的,而是专为处理HTTP(s)服务器请求而设计的,因此一些使其工作的代码可能取决于操作系统。当在本地运行并试图部署到我的远程服务器时,这是导致许多问题的原因。这都是我的经验,所以如果有人知道更多,请随时分享。

这就是我现在用来发送到https的代码。

# Remove www. RewriteBase / RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

但我以前也用过这些变体。

#Turn on https ( Cent OS ) RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Turn on https ( Mac OS ) RewriteCond %{HTTP:X-Forwarded-Proto} !https RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

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

您可以使用下面这样的代码来检测https,它将设置一个名为'HTTPS‘的全局常量布尔值

\define('HTTPS', ($_SERVER['HTTP_X_FORWARDED_PROTO'] ?? false) === 'https' || (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off');

这是php 7代码

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

票数 0
EN

Stack Overflow用户

发布于 2018-08-07 10:34:08

代码语言:javascript
运行
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51717245

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档