使用ssl / https将www重定向问题转换为非www

时间:2016-02-24 05:16:29

标签: php apache .htaccess redirect mod-rewrite

我有非www网址的SSL证书:https://domain.com

我知道有很多类似的问题,但它们似乎都没有解决我的问题。这是我想要做的:

问题:

1) http://www.domain.com -> https://domain.com **www to non www DONE**

2) http://domain.com -> https://domain.com **http to https DONE**

3) https://www.domain.com -> https://domain.com/ **NOT DONE**
      - Getting ERROR ON Above URL number (3): Your connection is not private

到目前为止,我的.htaccess文件看起来像这样:

# www to non www
RewriteCond %{HTTP_HOST} ^www\.domain\.com [NC]
RewriteRule (.*) https://domain.com/$1 [R=301,L]

# http to https
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# https://www to htttps://

请注意 .htaccess上面的代码对我的第一个和第二个问题工作正常,现在我想解决我的第三个问题

以下是我已经尝试过的解决方案:

3 个答案:

答案 0 :(得分:0)

试一试。如果这不起作用,请检查您的网站配置文件AllowOverride All。如果是None,请All

示例 site.conf

<Directory /var/www/directory/>
    Options Indexes
    AllowOverride All
    Order allow,deny
    allow from all
</Directory>

<强>的.htaccess

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.yourdomain.com [NC]
RewriteRule ^(.*)$ https://yourdomain.com/$1 [L,R=301]

答案 1 :(得分:0)

试试这个:

将www重定向到非www(使用SSL时)

RewriteCond %{HTTP_HOST} ^www.your_domain.com$
RewriteCond %{SERVER_PORT} ^443
RewriteRule ^(.*)$ https://your_domain.com/$1 [R=301]

最后2个用于使用SSL的在线商店,或者在某些页面上使用SSL时防止出现任何SSL错误。

UPDATE1:

RewriteEngine On

RewriteCond %{HTTPS} off
RewriteCond %{ENV:HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

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

更新2

RewriteEngine On 使所有http使用https:

RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://xxx.yyy/$1 [L,R=301]

仅使用www https使用非www https:

RewriteCond %{SERVER_PORT} 443
RewriteCond %{HTTP_HOST} ^www[.].+$
RewriteRule ^(.*)$ https://xxxx.yyy/$1 [L,R=301]

更新3:

点击此处查看此问题:

.htaccess redirect www to non-www with SSL/HTTPS

答案 2 :(得分:0)

尝试:

RewriteEngine on

RewriteCond %{HTTP_HOST} ^www\.(.+)$ [OR]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%1%{REQUEST_URI} [L,R]

上述规则将重定向

在测试之前清除浏览器的缓存。

相关问题