【问题标题】:laravel 4 rewrite url for paginationlaravel 4 重写 url 进行分页
【发布时间】:2014-02-02 00:55:41
【问题描述】:

我使用 Laravel 分页,但网址是这样的:

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

我更喜欢这样的网址:

http://mydomain.com/page/2

我有 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>

我试试这条线:

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

但它不起作用......有什么想法吗?可以使用 .htaccess 文件以外的其他内容吗?

【问题讨论】:

    标签: php .htaccess pagination laravel-4


    【解决方案1】:

    据我了解,Laravel 的默认分页系统不支持您正在寻找的那种路由。

    由于您的应用程序中的每个页面的路由看起来可能非常不同,因此很难在 .htaccess 中使用捕获所有的 RewriteRule 来以 Laravel 可以理解的格式正确重写 URL,同时也不会弄乱其他 URL 参数和路径。


    对于您的具体示例,但这仅适用于您的具体示例,没有其他效果,您可以试试这个:

    <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>
    

    同样,此不是通用解决方案,仅适用于 http://mydomain.com/page/2 之类的链接(不适用于 http://mydomain.com/users/page/2 之类的链接)

    【讨论】:

      猜你喜欢
      • 2013-08-24
      • 2013-05-30
      • 2013-12-24
      • 1970-01-01
      • 1970-01-01
      • 2015-02-09
      • 2011-03-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多