Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 pagination with trailing slash redirect to 301

I'm using Laravel 5 and notice that the pagination is adding a trailing slash before the ?page=# and with that, it always redirect to a 301 page.

http://example.com/news/articles/?page=2 will do a 301 redirect to http://example.com/news/articles?page=2

This is causing my pagination using ajax to slow down because it is having 2 responses.

How to make laravel accept http://example.com/news/articles/?page=2 so it won't make a 301 redirect?

I base it through this site which is using LengthAwarePaginator.

like image 533
basagabi Avatar asked Sep 10 '25 20:09

basagabi


2 Answers

If you look in your app/public/.htaccess file you will see this line:

# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]

By removing it you will disable trailing slash redirect.

like image 169
Sh1d0w Avatar answered Sep 13 '25 08:09

Sh1d0w


I'd do this in my controller instead of modifying .htaccess

    $posts= Article::latest()->paginate(4);
    $posts->setPath('');//just add this line after your paginate function

or some users might prefer to add this line when they generate links in view

$links = str_replace('/?', '?', $posts->render());
like image 35
Mahdi Younesi Avatar answered Sep 13 '25 08:09

Mahdi Younesi