使用.htaccess更改根文件夹目录并重定向到HTTPS

时间:2016-11-10 15:27:38

标签: .htaccess redirect ssl

我对.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文件,所以我无法将这两个功能集成在一起。任何有关这方面的帮助将不胜感激。感谢。

1 个答案:

答案 0 :(得分:0)

我很确定我自己也明白这一点。我只需将其添加到我的.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}