簡體   English   中英

.htaccess強制文件夾中的HTTPS

[英].htaccess force to HTTPS in a folder

我正在嘗試將論壇索引重定向為對所有內容都使用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等。 (請注意,我將網站網址更改為example.com和子目錄,僅在復制粘貼代碼中將其更改為子目錄)

我正在嘗試使網站顯示https的香蕉,但是每當我嘗試執行某些操作時,它都無法正常工作,並給我https://www.website.com//smf/index.php,並且我無法刪除// :(

無論如何,我可以清理代碼並使代碼更簡單易懂嗎?

謝謝!

我在.htaccess文件中度過了許多夜晚。但是,至此,這個文件實際上與php無關,但實際上與Apache相關。 這是一個獨立於PHP的程序(它本身就是一種編程語言和編譯器)。 而不是開始大量的切線,但這意味着您可以在沒有瀏覽器的終端中運行php。 任何麻煩,因為Apache並不是用PHP編程的,並且僅是為了處理HTTP服務器的請求而要求一些代碼使其工作,這可能取決於操作系統。 當在本地運行並嘗試部署到我的遠程服務器時,這是很多努力的原因。 這些都是我的經驗,因此,如果有人知道更多,請隨時分享。

這就是我現在用來發送到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代碼

然后,如果它為假,您將重定向您認為合適的方式。

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