简体   繁体   English

laravel 4重写分页URL

[英]laravel 4 rewrite url for pagination

I use Laravel pagination but the url are like this : 我使用Laravel分页,但网址是这样的:

http://mydomain.com/?page=2

I prefere to have url like this : 我更喜欢这样的网址:

http://mydomain.com/page/2

I have the htaccess : 我有htaccess:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

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

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]

</IfModule>

I try the line : 我尝试一下:

RewriteRule ^/([0-9])$ index.php?page=$1

But itt doesn't work... Any idea ? 但这不起作用...知道吗? Can use something else than the .htaccess file ? 可以使用.htaccess文件以外的其他文件吗?

As far as I understand Laravel's default pagination system does not support the kind of routing your are looking for. 据我了解,Laravel的默认分页系统不支持您要查找的那种路由。

Since your routes can look very differently for each page in your application is hard to come with a catch all RewriteRule in .htaccess that will properly rewrite the URL in a format that Laravel will understand while also not messing about with other URL parameters and paths. 因为您的应用程序中每个页面的路由看起来都非常不同,所以很难附带.htaccess中的所有RewriteRule,它们可以以Laravel可以理解的格式正确重写URL,同时也不会弄乱其他URL参数和路径。


For your specific example, but this will work ONLY for your specific example and nothing else , you could try this: 对于您的特定示例, 但这仅适用于您的特定示例,除此之外 ,您可以尝试以下操作:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

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

    #Handle pagination for urls like http://mydomain.com/page/2
    RewriteRule ^page/([0-9]+)$ index.php?page=$1

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]

</IfModule>

Again, this is not a general solution and will work only for links like http://mydomain.com/page/2 (and not for links like http://mydomain.com/users/page/2 ) 同样,这不是一般的解决方案,仅适用于http://mydomain.com/page/2之类的链接(不适用于http://mydomain.com/users/page/2之类的链接)

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

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