简体   繁体   中英

Remove index.php from URL, Laravel 5.2

I saw a lot of examples and tried a lot of solutions without success! I am trying to put my project to Ubuntu server with apache.

everything its ok when I do :

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

But I want:

"/MYEXAMPLE/public/dashboard"

And there is the problem!

"The requested URL /MYEXAMPLE/public/dashboard was not found on this server."

My apache server has de mod_rewrite.

My project folders are:

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

My .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}]

INDEX.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);

I do not want to remove my views from public folder! When I use wamp server, everything ok, I do not need to use the index.php at the URL. Now I am trying to use the Ubuntu server, but the URL's are not working because I need index.php

Other example: If I access

/MYEXAMPLE/public/

OR

/MYEXAMPLE/public/index.php

It goes to homepage as I want. The problem his when I am trying to change to other page!

Any sugestions to solve the problem? Why everything is ok running with wamp and when I try to use the Ubuntu server, I need the index.php at the URL?

Thanks in advance

You need to point web server to public directory and use normal URLs like /dashboard instead of /public/index.php/dashboard .

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

Also, you can use this 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>

there are two solution for this problem. 1.edit your .htaccess to

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.dont edit your code and only run in cmd

 php artisan serve

i offer you second solution for this problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM