简体   繁体   中英

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 .

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.

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

@shaddys answer is most properly the better solution, but I couldn't use it because of other routes. So I've just done it like this

$.ajax({
     url: url.replace('/?','?'),
      ....
});

With this you will get working pagination and no redirections.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM