让 Laravel htaccess 听我的重写

问题描述 投票:0回答:2

我正在尝试将一些旧网站页面重写为我闪亮的新 Laravel 网站上的路线。我原则上理解 .htaccess 是如何工作的,但是默认的 Laravel .htaccess 中有一行(我认为)正在抛出我自己的重写。所以,这就像我正在尝试做的事情:

RewriteRule ^user/(\d+)*$ ./users

但是,Laravel 已经有了这个:

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

这实际上表示“如果用户请求是针对任何不存在的文件或文件夹,则将其请求通过管道传输到index.php” - [L] 意味着这是这些条件的最后一条规则。

我的问题是,我应该把重写指令放在哪里?如果我把它放在该规则之后,那么我的重写肯定会被忽略,因为所有内容都已经发送到index.php(如果 htaccess 规则是连续的)

如果我将我的规则放在 Laravel 的默认规则之前,这是否意味着我的规则会搞乱 Laravel 想要做的事情?目前,无论我将规则放在哪里,我都会得到各种结果,从 500 错误到没有内容的空白页。

如何在不踩Laravel的情况下将自己的规则集成到Laravel的.htaccess文件中?

php apache .htaccess mod-rewrite laravel-5
2个回答
0
投票

为了确保 Laravel 的 .htaccess 文件规则不与您的规则冲突,您需要确保您的规则优先于他们的规则。我提供了 .htaccess 文件的示例,其中路径名已更改,每行都进行了注释,以便对面临相同问题的人有意义:

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

    # Make sure Rewrite engine is running
      RewriteEngine On

    # Make sure base of site is set to root (othwerwise you can get results like /var/www/public_html/ before the results pages)
      RewriteBase /

    # For all directory requests in the url (d) e.g. blah.com/directory/
      RewriteCond %{REQUEST_FILENAME} !-d

    # And for all file requests in the url (f) e.g. blah.com/directory/page.php
      RewriteCond %{REQUEST_FILENAME} !-f

    # Check for the CONDition that the page has 'product_id' in the url e.g. http:/www.blah.com/show_product.php?product_id=273
      RewriteCond %{QUERY_STRING} ^product_id=(.*)$
    # If the condition above was met, then check if the page is called list_products.php, if it is, redirect the browser to /product/ with the product id (273) as %1
      RewriteRule ^list_products.php$ product\/%1? [R=301,L]

    # Simpler redirect for pages without a url variable
      Redirect /car-hire.php https://www.blah.com/car-hire

    # Then comes Laravel’s .htaccess stuff
    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>

看起来(正如 CBroe 在他的评论中友好地回答的那样)将你的规则保持在 Laravel 的“之上”,就像上面那样就是答案


0
投票
选项-多视图-索引
RewriteEngine On

# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]

# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*) index.php?app=$1 [L,QSA]
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.