问题
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]
and the following routes:
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?
回答1:
If I understand your question correctly, you're trying to redirect all requests to http://example.com, http://www.example.com, and https://www.example.com to https://example.com. In order to do that, this is the proper way to do it in your access file.
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.example.com [OR]
RewriteCond %{SERVER_NAME} =example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
回答2:
About 1-st question (the reason of too many redirection):
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.
- Instead you can try these ways:
RewriteCond %{HTTPS} !=on RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
This will redirect all non-https requests to the with-https.
- If you want to redirect all non-www requests to the with-www, then use this:
RewriteCond %{HTTP_HOST} (www\.)?your.domain$ [NC] RewriteRule ^(.*)$ https://www.your.domain/$1 [R=301,L]
- If you want to mix these 2 conditions into one, you can do that via regex:
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/...
to the "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. Routes are not necessary for this question
来源:https://stackoverflow.com/questions/58209607/err-too-many-redirects-when-redirecting-to-non-www-https-in-laravel