禁用特定文件夹/路由的 Laravel 路由

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18075290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:13:53  来源:igfitidea点击:

Disable Laravel Routing for a specific folder/route

phplaravelweb-development-server

提问by Mitchell M

I'm wondering how to disable the routing on laravel for a specific directory?

我想知道如何禁用特定目录的 laravel 路由?

I am hoping to run my main website off of laravel (I'm rewriting it into the framework), but I'd like to retain my current forum software! The problem is, when laravel sees the "localhost/forums" it looks for a forums controller or route. I'd like it to simply go to the literal /forums directory..?

我希望从 laravel 运行我的主网站(我正在将它重写到框架中),但我想保留我当前的论坛软件!问题是,当 laravel 看到“localhost/forums”时,它会寻找论坛控制器或路由。我希望它只是转到文字 /forums 目录..?

回答by George D.

I had to add this line in public/.htaccess

我不得不在 public/.htaccess 中添加这一行

RewriteCond %{REQUEST_URI} !^/foldername

before this line (that redirects trailing slashes)

在此行之前(重定向尾部斜杠)

RewriteRule ^(.*)/$ / [L,R=301]

回答by KyleT

With Laravel 4 none of these solutions were working for me. I eventually solved it by placing

使用 Laravel 4,这些解决方案都不适合我。我最终通过放置解决了它

RewriteRule ^(directory-name) - [L]

before

RewriteRule ^(.*)/$ / [L,R=301]

in public/.htaccess

在公共/.htaccess

回答by Mike Rockétt

You need to make an exclusion in your .htaccessfile. Just above the rule that sends everything to index.php, add this:

您需要在.htaccess文件中排除。在将所有内容发送到 的规则上方index.php,添加以下内容:

RewriteCond %{REQUEST_URI} !^/forums

Anything that begins with /forumswill not be sent to Laravel.

任何以 开头的东西/forums都不会被发送到 Laravel。

(Of course, this is assuming you are using Apache.)

(当然,这是假设您使用的是 Apache。)

回答by Andreyco

According to your routes.phpfile it is supposed to work.

根据您的routes.php文件,它应该可以工作。

  1. You are not catching forumURL at all
  2. If 'forum' directory/file exists, URL is not rewritten to Laravel's index.php
  1. 你根本没有捕捉到forumURL
  2. 如果 'forum' 目录/文件存在,则 URL 不会重写到 Laravel 的 index.php

routes.php(I used this one in the test case)

routes.php(我在测试用例中使用了这个)

Route::get('{slug}', function($slug){
    return $slug;
});

File structure

文件结构

|- app
|- public
|--- index.php // Laravel's one
|--- forum // directory for forum files
|------ index.php // Forum's one
....

Using this structure, URL is not rewritten to Laravel's routing index.php file. If I rename/remove forumfolder URL is rewritten then.

使用这种结构,URL 不会被重写到 Laravel 的路由 index.php 文件中。如果我重命名/删除forum文件夹 URL 将被重写。

Does it work for you?

对你起作用吗?

回答by Ramin

I needed to exclude /billing url on Laravel 5.7 So this way worked for me:

我需要在 Laravel 5.7 上排除 /billing url 所以这种方式对我有用:

Route::redirect('/billing', '/billing');

when billing located at: My billing folder location

计费时位于: 我的计费文件夹位置

回答by Giang Nguyen

I'm using nginx, it's work for me:

我正在使用 nginx,它对我有用:

location /your_folder {     
    try_files $uri $uri/ /your_folder/index.php?$args;
}