HTTP to HTTPS 301 Redirection Code is Not Working, It says Too many redirects

风流意气都作罢 提交于 2019-12-12 16:12:02

问题


I am using Bluehost for one of my websites. Recently, Recently, i have moved my site from HTTP to HTTPS . After that, I have used So may different code including the below code to force HTTPS all over my website.

# SSL Rewrite
RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

After using this post, when I check redirection on redirect-checker available on online, I get following messages.

But it is not working. Too many redirects. Please try to reduce your number of redirects for http://www.example.com. You use 19 Redirects. Ideally, you should not use more than 3 Redirects in a redirect chain. More than three redirections will produce unnecessary load on your server and reduce speed, which ends up in bad user experience.

And If you want to access my website from a browser, I get warning as such:

The www.example.com page isn’t working. www.example.com redirected you too many times.

I think some redirection loop was creating such problem.

However,

My web site Status Right Now: Without using any code on htaccess

  1. https//www.example.com is working just fine. returning 200 OK. 200 OK2

  2. https//example.com is now 301 redirected to https//www.example.com (So, i think it is also just fine)

The problem is:

  1. http//example.com is now 301 redirected to http//www.example.com
  2. http//www. example.com is returning 200 OK.

To solve problem 3, I included Below code on my htaccess

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

After that I found that:

http//example.com is now 301 redirected to https//www.example.com

So the problem is solved for:

http//example.com
https//example.com
https//www.example.com

Now I just need some code to redirect 301 only:

http//www.example.com TO https//www.example.com`

Can anybody help me here?

And I am sorry if I explain my problem in a wired way... :D. The matter is I am not an expert guy on this. Hope you all will understand.


回答1:


Try this:

RewriteEngine On

# If not www
RewriteCond %{HTTP_HOST} !^www\. [NC]
# rewrite to https and www
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L,NE]

# If not HTTPS
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L,NE]


来源:https://stackoverflow.com/questions/42532560/http-to-https-301-redirection-code-is-not-working-it-says-too-many-redirects

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