【问题标题】:Change Root Folder Directory and Redirect to HTTPS with .htaccess使用 .htaccess 更改根文件夹目录并重定向到 HTTPS
【发布时间】:2016-11-11 06:18:49
【问题描述】:

我对 .htaccess 文件了解不多,需要一些帮助。我有几个网站托管了一个 CPanel 帐户。我不想将我的“主”站点直接存储在 public_html 文件夹中,所以我将它移到了一个子文件夹中,然后找到了这个 .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} !^/example_directory/ 
# 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 ^(.*)$ /example.com/$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 ^(/)?$ example.com/index.html

正如我所说,上面的 .htaccess 将允许我将我的站点文件存储在不同的文件夹中,而不是直接存储在根目录中。但现在我也试图通过 https 强制重定向该站点。我在这里找到了这个网站:

Force Website to Use SSL

但是,由于我真的不理解 .htaccess 文件,我无法将这两个功能集成在一起。对此的任何帮助将不胜感激。谢谢。

【问题讨论】:

    标签: .htaccess redirect ssl


    【解决方案1】:

    我很确定这是我自己想出来的。我只需要将它添加到我的 .htaccess 文件的末尾:

    RewriteCond %{HTTP_HOST} ^example\.com [NC]
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}
    

    它就像一个魅力。完整的 .htaccess 文件如下所示,它更改了站点使用的文件夹目录,然后强制它通过 https。

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^(www.)?example.com$
    RewriteCond %{REQUEST_URI} !^/example_directory/ 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /example.com/$1
    RewriteCond %{HTTP_HOST} ^(www.)?example.com$ 
    RewriteRule ^(/)?$ example.com/index.html
    RewriteCond %{HTTP_HOST} ^example\.com [NC]
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}
    

    【讨论】:

      猜你喜欢
      • 2019-04-30
      • 2017-01-29
      • 2021-09-03
      • 2011-01-19
      • 1970-01-01
      • 1970-01-01
      • 2011-01-14
      • 1970-01-01
      相关资源
      最近更新 更多