Htaccess:将所有请求重写为https,将域重写为子文件夹而不列出子文件夹

时间:2016-04-01 13:38:23

标签: apache .htaccess redirect mod-rewrite

我想在下面实现以下目标。

  1. 重写(www.example.com, example.com)至{https://example.com/folder)而未在网址中显示/folder,即(https://example.com

  2. 还希望其他请求(www.example.com/other-folders/..., example.com/other-folders/...)重写为https,即(https://example.com/other-folders/...

  3. 目前,我有以下内容:

    RewriteEngine On
    RewriteCond %{HTTPS} !on
    RewriteRule (.*) https://%{HTTP_HOST}
    
    RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
    RewriteCond %{HTTP_HOST} ^www\.example\.com$
    RewriteCond %{REQUEST_URI} ^/$
    RewriteRule (.*)$ /folder/$1
    
      

    www.example.com - 完美无缺

         

    example.com - 工作但返回此错误“检测到错误的访问权限,此服务器只能通过https://example.com/folder访问... 然后重定向但是显示/folder

         

    www.example.com/other-foldersexample.com/other-folders - 不起作用

1 个答案:

答案 0 :(得分:0)

尝试:

RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}/$1 [L,R]

RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteCond %{REQUEST_URI} !^/folder
RewriteRule (.*)$ /folder/$1