【问题标题】:Laravel skip directory from frameworkLaravel 从框架中跳过目录
【发布时间】:2015-07-13 02:17:52
【问题描述】:

我遇到了一个问题,我的网站的映射如下所示:

- app
- bootstrap
- public_html
  - archives
  - css
  - js
  index.php
- vendor
composer.json
etc...

我的 .htaccess 位于public_html。我想从整个框架中跳过目录public_html/archives。我该怎么做?我希望路由 mysite.com/archives 在 Laravel 之外可以访问。

编辑: 这是我的 htaccess 文件

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

RewriteEngine On

# Redirect Trailing Slashes...
RewriteCond %{REQUEST_URI} "/public/archives/" <--- ADDED!
RewriteRule ^(.*)/$ /$1 [L,R=301]

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

【问题讨论】:

  • 你试过了吗?因为使用默认的.htaccess Laravel 不会在对实际存在的文件或目录的请求上运行。
  • 我用 .htaccess 文件尝试了很多东西,但它似乎不起作用。
  • @Displee - 请分享您的.htaccess 文件的内容。您是否对文件进行了更改?
  • 我已将我的 htaccess 文件添加到主帖中,抱歉回复晚了。

标签: php .htaccess laravel-4 laravel-routing


【解决方案1】:

正如其他人所指出的,由于以下规则,Laravel 不会处理任何存在的文件或目录:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

首先,将此规则从您的 .htaccess 文件中删除:

RewriteCond %{REQUEST_URI} "/public/archives/"

那么,在RewriteEngine On这一行之后,你需要添加这条规则:

RewriteRule ^archives/?$ - [PT]

所以您的 .htaccess 文件将如下所示:

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

    RewriteEngine On

    RewriteRule ^archives/?$ - [PT]

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

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

或者,您可以在RewriteCond %{REQUEST_FILENAME} !-d 之前添加以下行:

RewriteCond %{REQUEST_URI} !^/archives/?.*$

【讨论】:

    猜你喜欢
    • 2013-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-24
    • 2014-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多