【问题标题】:Remove index.php from URL, Laravel 5.2从 URL 中删除 index.php,Laravel 5.2
【发布时间】:2017-01-28 14:10:42
【问题描述】:

我看了很多例子,尝试了很多解决方案都没有成功! 我正在尝试使用 apache 将我的项目放到 Ubuntu 服务器上。

一切正常:

"/MYEXAMPLE/public/index.php/dashboard" 

但我想要:

"/MYEXAMPLE/public/dashboard"

还有问题!

“在此服务器上找不到请求的 URL /MYEXAMPLE/public/dashboard。”

我的 apache 服务器 有 de mod_rewrite。

我的项目文件夹是:

     - MYEXAMPLE
     --------- server.php
     --------- public
     ----------------.htaccess.php 
     ----------------.index.php 
     ---------------- views

我的.htaccess.php:

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

RewriteEngine On

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

# 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}]

索引.PHP:

<?php //


require __DIR__ . '/../bootstrap/autoload.php';


$app = require_once __DIR__ . '/../bootstrap/app.php';


$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);

$response = $kernel->handle(
        $request = Illuminate\Http\Request::capture()
);

$response->send();

$kernel->terminate($request, $response);

我不想从公共文件夹中删除我的视图!当我使用 wamp 服务器时,一切正常,我不需要在 URL 处使用 index.php。现在我正在尝试使用 Ubuntu 服务器,但是 URL 不起作用,因为我需要 index.php

其他例子:如果我访问

/MYEXAMPLE/public/

/MYEXAMPLE/public/index.php

它会按我的意愿进入主页。当我试图切换到其他页面时,他的问题!

有解决问题的建议吗?为什么使用 wamp 运行一切正常,而当我尝试使用 Ubuntu 服务器时,我需要 URL 中的 index.php?

提前致谢

【问题讨论】:

标签: php .htaccess laravel mod-rewrite laravel-5.2


【解决方案1】:

您需要point web server to public directory 并使用/dashboard 等普通网址而不是/public/index.php/dashboard

DocumentRoot "/path_to_laravel_project/public"
<Directory "/path_to_laravel_project/public">

另外,你可以使用这个working Apache virtual host configuration for Laravel

<VirtualHost some.app:80>
    DocumentRoot "/path_to_laravel_project/public"
    ServerName some.app
    ServerAlias some.app

    <Directory "/path_to_laravel_project/public">
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

【讨论】:

    【解决方案2】:

    这个问题有两种解决方案。 1.编辑你的.htaccess到

    DirectoryIndex index.php
    
    <IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>
    
    RewriteEngine On
    
    RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
    RewriteRule ^(.*) - [E=BASE:%1]
    
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^index\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]
    
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule .? - [L]
    
    RewriteRule .? %{ENV:BASE}/index.php [L]
    

    2.不要编辑你的代码,只在cmd中运行

     php artisan serve
    

    我为您提供解决此问题的第二种方法

    【讨论】:

      猜你喜欢
      • 2018-03-04
      • 1970-01-01
      • 1970-01-01
      • 2019-08-16
      • 2017-09-28
      • 1970-01-01
      • 2014-05-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多