2017-01-22 80 views
1

试图将所有http和none-www的流量重定向到https(带或不带www)。 使用.htaccessapache VirtualHost文件:apache重定向http到https而没有www

虚拟主机文件

<VirtualHost *:80> 
    ServerName adi-md.co.il 
    ServerAlias www.adi-md.co.il 

的.htaccess

<IfModule mod_rewrite.c> 
RewriteEngine On 
RewriteBase/
RewriteCond %{HTTPS} =on 
RewriteCond %{HTTP_HOST} ^www\. [NC,OR] 
RewriteCond %{HTTP_HOST} ^adi-md.co.il 
#RewriteRule^https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L,NE] 
#RewriteCond %{HTTP_HOST} ^(?:www\.)?(.*)$ [NC] 
#RewriteRule (.*) https://www.%1%{REQUEST_URI} [L,R=301] 
#RewriteCond %{HTTP_HOST} ^www.adi-md.co.il 

选项1

RewriteCond %{HTTPS} =on 
RewriteCond %{HTTP_HOST} ^adi-md.co.il 

选项2

RewriteCond %{HTTPS} off 
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.*)$ [NC] 
RewriteRule (.*) https://www.%1%{REQUEST_URI} [L,R=301] 

打字的时候只是adi-md.co.il你的服务器的默认页面。

我该如何解决这个问题,这样所有的流量都被重定向到https以及none-www请求?

注: 我已经看过这3个,但他们没有提供答案(2出3处理WWW到无www的)

apache redirect http to https and www to non www
http to https apache redirection
Redirect from http to https without www using .htaccess

+0

结帐http://redirect-www.org/ –

回答

0

试试这个:

RewriteCond %{HTTP_HOST} ^www\. [NC,OR] 
RewriteCond %{HTTPS} =on 
RewriteRule^http://%{SERVER_NAME}%{REQUEST_URI} [R=301,L,NE] 

这基本上会首先检查您是否已启用www并开启HTTPs。如果其中任何一个都会将其切换回HTTP并删除www

确保在测试之前清除缓存。对于HTTP重定向到https

+0

不知何故这并没有做的工作......我更新了我的问题的'。 htaccess'文件 – Jadeye

1

两个简单的规则加www:

RewriteCond %{HTTP_HOST} ^www\.(.+) [NC] 
RewriteRule^https://%1%{REQUEST_URI} [L,R=301] 

RewriteCond %{HTTPS} off 
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] 
+0

是否可以使用这些规则。但在使用.local域名主机时忽略它们? – Floris