【问题标题】:Laravel subdirectory root not accessible?Laravel 子目录根无法访问?
【发布时间】:2016-06-21 03:06:46
【问题描述】:

我相信有人可以在大约 2 秒内回答的快速问题。

我设置了一个 Laravel 项目,其中一个子目录设置为文档。

我设置了以下代码。

Route::group(['prefix' => 'docs'], function(){
    route::get('/', function(){
       return redirect('docs.intro');
    });

    Route::get('/intro', ['as' => 'docs.intro', function(){ return view('docs.intro'); }]);
});

当我导航到“www.site.com/docs/intro”时,一切正常。但是,当我导航到“www.site.com/docs/”时,它并没有像我希望的那样重定向,而是抛出 403“禁止”错误。有人知道为什么吗?我怎样才能使它正常工作?

谢谢!

【问题讨论】:

  • 尝试写'prefix' => '/docs'
  • 谢谢,但这没有帮助。

标签: php .htaccess laravel laravel-5 routes


【解决方案1】:

那是因为你指向一个存在的目录,所以你的网络服务器没有将请求转发到 Laravel 的index.phpfile。

检查您的 apache / nginx 配置并查找重写规则。如果您使用的是 apache,您可能会发现类似以下内容:

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

这意味着如果目录 (-d) 或文件 (-f) 存在,则不会发生重写。

【讨论】:

  • 我已将此标记为正确,但正确的原因是准确的。这是我的更新,以便其他人可以学习。如果“docs”,我试图导航到 URI,但因为我的公共 LARAVEL 文件夹中也有一个“docs”文件夹,所以它导致了冲突。本质上,laravel 应用程序正在与 apache 混淆,因为公用文件夹与 URI 具有相同的名称。人们 -> 不要那样做!
【解决方案2】:

如果您使用的是 apache,请运行 apache2 -v 如果是 Apache 2.4.x+

  1. 改变

Options Indexes FollowSymLinks MultiViewsOptions +Indexes +FollowSymLinks +MultiViews

  1. 改变

    Order allow,deny Allow from allRequire all granted

不是 2.4.X 在 htaccess 中试试这个

Options +FollowSymLinks
RewriteEngine On

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

如果你使用的是 nginx:

  1. SSH 到你的服务器
  2. sudo nano /etc/nginx/sites-available/default
  3. 将服务器块修改为如下所示

    server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;
    
    root /var/www/laravel/public;    **//Modify this according to your project**
    index index.php index.html index.htm;
    
    server_name server_domain_or_IP; **//Modify**
    
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    
    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
    }
    
    1. sudo service nginx restart

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-05
    • 2012-09-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多