當我使用https://example.com它打開網站時,它會public在沒有 URL 的情況下打開,但是當我嘗試打開http://example.com它時,它將重定向到https://example.com/public,public在 URL 中。
我想重定向http://到https://但不在publicURL 中。
檔案夾結構:

.htaccess 檔案代碼,位于根目錄
#disable directory browsing
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
#PROTECT ENV FILE
<Files .env>
order allow,deny
Deny from all
</Files>
#PROTECT ENV FILE
<Files .htaccess>
order allow,deny
Deny from all
</Files>
# BEGIN cPanel-generated php ini directives, do not edit
# Manual editing of this file may result in unexpected behavior.
# To make changes to this file, use the cPanel MultiPHP INI Editor (Home >> Software >> MultiPHP INI Editor)
# For more information, read our documentation (https://go.cpanel.net/EA4ModifyINI)
<IfModule php7_module>
php_flag display_errors Off
php_value max_execution_time 30
php_value max_input_time 60
php_value max_input_vars 1000
php_value memory_limit -1
php_value post_max_size 8M
php_value session.gc_maxlifetime 1440
php_value session.save_path "/var/cpanel/php/sessions/ea-php74"
php_value upload_max_filesize 2M
php_flag zlib.output_compression Off
</IfModule>
<IfModule lsapi_module>
php_flag display_errors Off
php_value max_execution_time 30
php_value max_input_time 60
php_value max_input_vars 1000
php_value memory_limit -1
php_value post_max_size 8M
php_value session.gc_maxlifetime 1440
php_value session.save_path "/var/cpanel/php/sessions/ea-php74"
php_value upload_max_filesize 2M
php_flag zlib.output_compression Off
</IfModule>
# END cPanel-generated php ini directives, do not edit
# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php74” package as the default “PHP” programming language.
<IfModule mime_module>
AddHandler application/x-httpd-ea-php74 .php .php7 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit
公共目錄中的 .htaccess 檔案代碼
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (. )/$
RewriteRule ^ %1 [L,R=301]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
uj5u.com熱心網友回復:
您發布的代碼中沒有 HTTP 到 HTTPS 重定向。如果這是在服務器配置中較早執行的,則需要在服務器配置中進行更正。但是,如果它稍后在您的應用程式中執行(不正確),那么您可以通過在.htaccess. 這需要放在/public/.htaccess檔案的頂部:
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://example.com/$1 [R=301,L]
(這確實假設 SSL 證書直接安裝在您的應用程式服務器上,并且您沒有使用代理來管理您的 SSL 連接。如果是這樣,則需要調整條件。)
由于這是在/public/.htaccess檔案中,因此捕獲的反向參考$1自然會排除/public/子目錄,因此將其排除在重定向之外。
您應該首先使用 302(臨時)重定向進行測驗,以避免潛在的快取問題。并確保您在測驗前清除了瀏覽器快取。
但是,以下規則是有問題的,并且/public如果您請求帶有尾部斜杠的 URL,也會暴露子目錄:
# Redirect Trailing Slashes If Not A Folder... RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} (. )/$ RewriteRule ^ %1 [L,R=301]
如果它在根.htaccess檔案中,則該規則可以,但是,如果它在/public/.htaccess檔案中(它需要),那么REQUEST_URI服務器變數包含/public/目錄(在內部重寫請求之后)。您正在從變數中捕獲 URL 路徑。
這條規則應該這樣寫:
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (. )/$ /$1 [L,R=301]
這也稍微更有效并且無論.htaccess它放在哪個檔案中都有效(盡管它應該在/public/.htaccess檔案中,而不是 root .htaccess)。另外:如果/public子目錄沒有從 URL 中隱藏,那么這將不起作用,但這里不是這種情況。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/409923.html
標籤:
上一篇:如何對index.html進行301重定向并強制使用HTTPS?
下一篇:將CSV檔案決議為字典C#
