简体   繁体   English

.htaccess强制文件夹中的HTTPS

[英].htaccess force to HTTPS in a folder

I am trying to redirect a forum index to use HTTPS for everything, but .htaccess is pretty new to me and i am learning so anyone could give a hand i would appreciate it :) 我正在尝试将论坛索引重定向为对所有内容都使用HTTPS,但是.htaccess对我来说还很新,我正在学习,所以任何人都可以伸出援手,我将不胜感激:)

So i have the forum in a subdirectory called SMF. 因此,我在名为SMF的子目录中拥有该论坛。 Currently everything is going to www.website.net/smf/index.php 目前,所有内容都将转到www.website.net/smf/index.php

But i wanted to redirect everything to HTTPS, while keeping the /smf/index.php structure. 但是我想将所有内容重定向到HTTPS,同时保持/smf/index.php结构。

Here is the current .htaccess i have in my main html_public: 这是我在主要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] 

When i change ANYTHING, everything goes bananas. 当我改变任何东西时,一切都会变成香蕉。 Everything in the forum is in subdirectory folder, including the index.php and so on. 论坛中的所有内容都在子目录文件夹中,包括index.php等。 (please note i changed the website url to example.com and the subdirectory, to subdirectory only in copy paste code) (请注意,我将网站网址更改为example.com和子目录,仅在复制粘贴代码中将其更改为子目录)

I am going bananas trying to make the website show https, but whenever i try something it doesnt work and gives me https://www.website.com//smf/index.php and i cant remove the // :( 我正在尝试使网站显示https的香蕉,但是每当我尝试执行某些操作时,它都无法正常工作,并给我https://www.website.com//smf/index.php,并且我无法删除// :(

Is there anyway i can clean the code and make it more simple and easier to understand? 无论如何,我可以清理代码并使代码更简单易懂吗?

Thanks! 谢谢!

I have had many nights struggling around the .htaccess file... But heres the deal, this file actually has nothing to do with php, but is actually associated with Apache. 我在.htaccess文件中度过了许多夜晚。但是,至此,这个文件实际上与php无关,但实际上与Apache相关。 This is a separate program from PHP (which is its self a programing language and compiler). 这是一个独立于PHP的程序(它本身就是一种编程语言和编译器)。 And not to get off on to much of a tangent, but this means you can run php in a terminal with out a browser. 而不是开始大量的切线,但这意味着您可以在没有浏览器的终端中运行php。 Any hoot, because Apache isn't programmed in PHP, and is solely designed to handle HTTP(s) server requests some of the code to make it work may be operating system dependent. 任何麻烦,因为Apache并不是用PHP编程的,并且仅是为了处理HTTP服务器的请求而要求一些代码使其工作,这可能取决于操作系统。 This was the cause of a lot of struggle when running locally and trying to deploy to my remote server. 当在本地运行并尝试部署到我的远程服务器时,这是很多努力的原因。 This is all just from my experience, so if someone knows more please feel free to share. 这些都是我的经验,因此,如果有人知道更多,请随时分享。

This is what I use now to send to https. 这就是我现在用来发送到https的内容。

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

But I have used these variants before too. 但是我也曾经使用过这些变体。

#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]

I recommend for portability to make this feature yourself in php directly. 我建议您具有可移植性,以便直接在php中自行实现此功能。

You can detect https using something like this below, which will set a global constant Boolean named 'HTTPS' 您可以使用以下类似方法检测https,这将设置一个名为“ HTTPS”的全局常量布尔值

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

This is php 7 code 这是PHP 7代码

Then you'd just redirect how you see fit if it false. 然后,如果它为假,您将重定向您认为合适的方式。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM