在Laravel中重定向到非www HTTPS时出现ERR_TOO_MANY_REDIRECTS [英] ERR_TOO_MANY_REDIRECTS when redirecting to non-www HTTPS in Laravel

查看:184
本文介绍了在Laravel中重定向到非www HTTPS时出现ERR_TOO_MANY_REDIRECTS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将所有呼叫重定向到我网站上的非www HTTPS,除了自动生成的内容外,在Laravel的.htaccess文件中还有以下代码:

I am trying to redirect all calls to non-www HTTPS on my website and have the following code in my .htaccess file in Laravel besides the auto generated stuff:

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

以及以下路线:

Route::get('/', function () {
    return redirect()->route('build.index');
});

Auth::routes();

// Profile
Route::get('/profile/{user}', 'ProfilesController@show')->name('profile.show');
Route::get('/profile/{user}/edit', 'ProfilesController@edit')->name('profile.edit');

// Build
Route::get('/builds', 'BuildsController@index')->name('build.index');
Route::get('/builds/{hunter}', 'BuildsController@hunter')->name('build.hunter');
Route::get('/build/create', 'BuildsController@create')->name('build.create');
Route::post('/build', 'BuildsController@store')->name('build.store');
Route::get('/build/{build}/edit', 'BuildsController@edit')->name('build.edit');
Route::put('/build/{build}', 'BuildsController@update')->name('build.update');
Route::get('/build/{build}/{hunter?}/{title?}', 'BuildsController@show')->name('build.show');

// Skill trees
Route::get('/planner/{encryption}', 'PlannerController@show');
Route::get('/planner', 'PlannerController@index');

Route::post('/rate/{build}/{rating}', 'RatingsController@store');
Route::put('/rate/{build}/{rating}', 'RatingsController@update');
Route::delete('/rate/{build}', 'RatingsController@destroy');

Route::post('/comment', 'CommentsController@store');

有人知道是什么导致此重定向循环吗?

Does someone know what is causing this redirect loop?

推荐答案

关于第一个问题(重定向过多的原因):

About 1-st question (the reason of too many redirection):

重定向来自.htaccess的第3行 可能您输入了非"www" URL,它想将其重定向到页面( https://example.com/ ...(以您的情况为准),该前缀也没有"www"前缀. 因此它陷入了循环,并在20次之后抛出了错误.

Redirects are comes from 3-rd line of your .htaccess Probably you've typed non-"www" URL and it wanted to redirect that to the page (https://example.com/... in your case) which is also not have "www" prefix. So it falls in loop, and after 20 times it throwed an error.

  1. 相反,您可以尝试以下方式:

RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

这会将所有非https请求重定向到with-https.

This will redirect all non-https requests to the with-https.

  1. 如果您要将所有非www请求重定向到with-www,请使用以下命令:

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

  1. 如果您想将这两个条件混合在一起,可以通过正则表达式来实现:

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

这将重定向所有这些请求

This will redirect all these requests

 http://your.domain/...
 http://www.your.domain/...
 https://your.domain/...

到" https://www.your.domain/ ..."

注意:不要忘记在这三种方法之前写这篇文章.例如这样的

Note: don't forget to write this right before your these 3 ways. for example like this:

 # actually this can be the answer for your 2-nd question
 RewriteEngine On

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

P.S.对于这个问题,路线不是必需的

P.S. Routes are not necessary for this question

这篇关于在Laravel中重定向到非www HTTPS时出现ERR_TOO_MANY_REDIRECTS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆