简体   繁体   English

Laravel 5用尾部斜线分页重定向到301

[英]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. 我正在使用Laravel 5,并注意到分页是在?page=#之前添加一个尾部斜杠,并且它总是重定向到301页面。

http://example.com/news/articles/?page=2 will do a 301 redirect to http://example.com/news/articles?page=2 http://example.com/news/articles/?page=2会进行301重定向到http://example.com/news/articles?page=2

This is causing my pagination using ajax to slow down because it is having 2 responses. 这导致我使用ajax减速,因为它有2个响应。

How to make laravel accept http://example.com/news/articles/?page=2 so it won't make a 301 redirect? 如何让laravel接受http://example.com/news/articles/?page=2这样它不会进行301重定向?

I base it through this site which is using LengthAwarePaginator . 我通过这个使用LengthAwarePaginator网站建立它。

If you look in your app/public/.htaccess file you will see this line: 如果您查看app/public/.htaccess文件,您会看到以下行:

# 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 我会在我的控制器中执行此操作, 而不是修改.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. @shaddys答案是最合适的解决方案,但由于其他路由我无法使用它。 So I've just done it like this 所以我就这样做了

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

With this you will get working pagination and no redirections. 有了这个,你将获得工作分页,没有重定向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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