redirection issue in www to non www with ssl/https

落爺英雄遲暮 提交于 2020-01-03 04:27:08

问题


I have a SSL certificate for non www URL: https://domain.com

I know there are many similar question, but none of them seems to solve my problem. Here is what I am trying to do:

Problem:

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

So far, my .htaccess file looks like so:

# 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://

Please note that above code of .htaccess is working fine for my first and second problem, Now I want to solve my third problem

Here are the solutions that I already tried:

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

  • htaccess force https and redirect www to non-www, but no other subdomains

  • MANY MORE


回答1:


Try this out:

To redirect www to non-www (while using SSL)

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

The last 2 are meant for online stores using SSL or to prevent any SSL errors while using SSL on some pages.

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]

Update 2

RewriteEngine On Make all http use https:

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

Make only www https use the non-www https:

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

Update 3:

Check out this issue over here:

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




回答2:


Try :

RewriteEngine on

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

The rule above will redirect

  • http://www.example.com/

to

  • https://example.com/

And

  • https://www.example.com/

to

  • https://example.com/

Clear your browser's cache before testing this.




回答3:


Try this out. If this doesn't work then check in your site conf file you have AllowOverride All. If it is None make it All

Sample 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]


来源:https://stackoverflow.com/questions/35593803/redirection-issue-in-www-to-non-www-with-ssl-https

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!