【发布时间】:2018-07-17 19:49:01
【问题描述】:
我在正确设置 htaccess 时遇到问题,希望能得到一些帮助以解决问题。目前,我使用 /public_html/ 文件夹中的以下 htaccess 将我的共享主机上的主域路由到 /public_html/example.com(为了保持一致性):
# .htaccess main domain to subdirectory redirect
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.com/
# 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/ [L]
这一切都很好,但现在我想添加行来强制 WWW 和强制 HTTPS。我有一个非常适合我的子域的 htaccess 文件,但是当我将它添加到我的 /public_html/example.com/.htaccess 文件时,它会产生奇怪的行为。任何到 example.com 的流量都会重定向到 https://www.example.com/example.com。这段代码如下所示。
#First rewrite any request to the wrong domain to use the correct one (here www.)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#Now, rewrite to HTTPS:
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
谁能帮我合并这两者,以便我将目录重定位到 /public_html/example.com,一次性强制 WWW 和强制 HTTPS?非常感谢您!
【问题讨论】:
标签: .htaccess dns shared-hosting