【问题标题】:ERR_TOO_MANY_REDIRECTS when redirecting to non-www HTTPS in Laravel在 Laravel 中重定向到非 www HTTPS 时出现 ERR_TOO_MANY_REDIRECTS
【发布时间】:2019-10-03 07:00:44
【问题描述】:

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

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');

有人知道导致此重定向循环的原因吗?

【问题讨论】:

    标签: php laravel .htaccess


    【解决方案1】:

    如果我正确理解您的问题,您正在尝试将所有请求重定向到 http://example.comhttp://www.example.comhttps://www.example.comhttps://example.com。为此,这是在您的访问文件中执行此操作的正确方法。

    RewriteEngine on
    RewriteCond %{SERVER_NAME} =www.example.com [OR]
    RewriteCond %{SERVER_NAME} =example.com
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
    

    【讨论】:

    • 我试过了,仍然得到重定向,甚至直接在 Apache .conf 文件中尝试过,但没有成功。
    【解决方案2】:

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

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

    1. 您可以尝试以下方法:
    RewriteCond %{HTTPS} !=on
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    

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

    1. 如果要将所有非 www 请求重定向到 with-www,请使用以下命令:
    RewriteCond %{HTTP_HOST} (www\.)?your.domain$ [NC]
    RewriteRule ^(.*)$ https://www.your.domain/$1 [R=301,L]
    
    1. 如果您想将这 2 个条件合二为一,可以通过正则表达式实现:
    RewriteCond %{HTTP_HOST} (www\.)?(.*) [NC]
    RewriteRule ^(.*)$ https://www.%2/$1 [R=301,L]
    

    这将重定向所有这些请求

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

    到“https://www.your.domain/...”

    注意:别忘了在这三种方式之前写下。比如这样:

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

    附:这个问题不需要路线

    【讨论】:

    • 我尝试了您的第一个解决方案,但仍然出现错误。在“网络”选项卡中,我可以看到它只是出于某种原因不断刷新相同的 https 页面。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-22
    • 1970-01-01
    • 1970-01-01
    • 2016-09-09
    • 2016-04-27
    • 1970-01-01
    相关资源
    最近更新 更多