htaccess-协助强制WWW和HTTPS。当前结果为https://www.example.com/example.com

时间:2018-07-17 19:49:01

标签: .htaccess dns shared-hosting

我在正确设置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?提前非常感谢您!

1 个答案:

答案 0 :(得分:0)

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

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

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

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

买家的选择。