【问题标题】:htaccess - assistance forcing WWW and HTTPS. Currently results in https://www.example.com/example.comhtaccess - 帮助强制使用 WWW 和 HTTPS。当前结果为 https://www.example.com/example.com
【发布时间】: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


    【解决方案1】:

    这是由您的 .htaccess 中的另一条规则引起的。有问题的规则是:

    RewriteRule ^(/)?$ example.com/ [L]
    

    或者更准确地说是规则末尾的[L] 标志。代表LAST。这是告诉您的.htaccess 停止任何规则通过此重写规则。

    您可以通过将新规则置于 .htaccess 顶部、删除 [L] 标志或完全删除其他规则来解决此问题。

    买家选择。

    【讨论】:

    • 谢谢。我拿出了[L],然后在底部添加了www/https规则。似乎运作良好。
    猜你喜欢
    • 2019-12-04
    • 2016-01-16
    • 2014-03-04
    • 2013-05-31
    • 2016-04-05
    • 2011-10-14
    • 1970-01-01
    • 1970-01-01
    • 2016-04-29
    相关资源
    最近更新 更多