如何将所有URL指向https www版本

在我的Drupal的httaccess文件中,我希望每个URL都可能最终redirect到https www site org(注意'和'www')

我可以得到以下工作:

  1. www.site自动redirect到https www.site
  2. http www.siteredirect到https www.site

我无法得到:

  1. https站点redirect到https www.site – 而是转到https站点
  2. http站点自动redirect到https www.site – 而是转到https站点
  3. 网站自动redirect到https的网站 – 而是去https网站

到目前为止,这里是httaccess代码:

# Set "protossl" to "s" if we were accessed via https://. This is used later # if you enable "www." stripping or enforcement, in order to ensure that # you don't bounce between http and https. RewriteEngine on RewriteRule ^ - [E=protossl:s] RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] # If your site can be accessed both with and without the 'www.' prefix, you # can use one of the following settings to redirect users to your preferred # URL, either WITH or WITHOUT the 'www.' prefix. Choose ONLY one option: # # To redirect all users to access the site WITH the 'www.' prefix, # (http://example.com/foo will be redirected to http://www.example.com/foo) # uncomment the following: # RewriteCond %{HTTP_HOST} . # RewriteCond %{HTTP_HOST} !^www\. [NC] # RewriteRule ^ http%{ENV:protossl}://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 

感谢您的帮助

 # RewriteCond %{HTTP_HOST} . # RewriteCond %{HTTP_HOST} !^www\. [NC] # RewriteRule ^ http%{ENV:protossl}://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 

在你发布的代码中,最后一部分( 注释掉 )看起来应该做你所需要的 – 所以你只需要取消注释最后3行!?

但是,您还应该更改这些指令的顺序,以避免在请求http://example.com表单的URL时发生多重redirect。

例如:

 RewriteEngine on # Set "protossl" to "s" if we were accessed via https://. This is used later # if you enable "www." stripping or enforcement, in order to ensure that # you don't bounce between http and https. RewriteRule ^ - [E=protossl:s] # If your site can be accessed both with and without the 'www.' prefix, you # can use one of the following settings to redirect users to your preferred # URL, either WITH or WITHOUT the 'www.' prefix. Choose ONLY one option: # # To redirect all users to access the site WITH the 'www.' prefix, # (http://example.com/foo will be redirected to http://www.example.com/foo) # uncomment the following: RewriteCond %{HTTP_HOST} . RewriteCond %{HTTP_HOST} !^www\. [NC] RewriteRule ^ http%{ENV:protossl}://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301] # HTTP to HTTPS redirect (By this stage the HTTP_HOST is already canonicalised) RewriteCond %{HTTPS} off RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] # Other... RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] 

与往常一样,在testing之前清除浏览器caching。