首页 文章

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

提问于
浏览
0

我需要将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 . 提前致谢!

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]
    

相关问题