.htaccess重写为https和隐藏的子文件夹

问题描述 投票:0回答:1

我需要将http://www.example.comhttp://example.com重写为https://www.example.com。另一个复杂性是这些URL不是指向索引文件,而是指向/文件夹。即https://www.example.com真正指向/ var / www / html /文件夹,而不在URL栏中显示https://www.example.com/folder

我曾经设法获得第二部分

RewriteCond %{HTTP_HOST} www.example.com [NC,OR]
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ folder/$1 [L]

我知道第一部分可以用这样的东西来实现:

RewriteCond     %{SERVER_PORT} ^80$
RewriteRule     ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]

但不知何故,我不能让他们两个一起工作,因为我不熟悉htaccess。提前致谢!

apache .htaccess url-rewriting
1个回答
1
投票

您可以使用单个规则添加wwwhttp -> https重定向,然后为文件夹转发重写规则:

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]

RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.com$ [NC]
RewriteRule ^(?!folder/)(.*)$ folder/$1 [L,NC]
© www.soinside.com 2019 - 2024. All rights reserved.