简体   繁体   中英

laravel url requires index.php

I'm developing my 2nd Laravel app on my local machine. The first one has no issues with the url (it can use index.php or not) but my second one throws a 404 if I don't use index.php.

I copied the apache conf file and edited in the url so it is identical to the original in all other aspects. Apache mod rewrite is enabled (the other site works without index.php)

The only difference is that the new site is in laravel 8 whereas my working other laravel app is using laravel 7

.htaccess is the standard laravel file. My conf files are using the public folder in both instances. So why is this different? Have I missed something obvoius?

thanks

Craig

*** EDIT *** @Brian Thompson: This is on a dev machine apache permissions have been added to my user so can just use {USER} As requested here is some code (let me know if you need more than my project.conf and .htaccess files): .htaccess in {doc_root)/public (unchanged from when created and identical to the one from Laravel7)

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

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

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

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

and my apache conf file (which is a copy of the working one with the server names and document roots changed)

<IfModule mod_ssl.c>
        <VirtualHost _default_:443>
                ServerAdmin me@my-email.com
                ServerName laravel2.tst

                DocumentRoot /var/www/laravel2.tst/htdocs/public
                        <Directory /var/www/laravel2.tst/htdocs/public>
                                Options Indexes FollowSymLinks
                                AllowOverride All
                        </Directory>
                ErrorLog /var/www/laravel2.tst/error.log
                CustomLog ${APACHE_LOG_DIR}/access.log combined

                SSLEngine on

                SSLCertificateFile      /etc/ssl/certs/apache-selfsigned.crt
                SSLCertificateKeyFile   /etc/ssl/private/apache-selfsigned.key

                <FilesMatch "\.(cgi|shtml|phtml|php)$">
                                SSLOptions +StdEnvVars
                </FilesMatch>
                <Directory /usr/lib/cgi-bin>
                                SSLOptions +StdEnvVars
                </Directory>

                BrowserMatch "MSIE [2-6]" \
                               nokeepalive ssl-unclean-shutdown \
                               downgrade-1.0 force-response-1.0

        </VirtualHost>
</IfModule>

<VirtualHost *:80>
    ServerName laravel2.tst
    ServerAlias laravel2.bup
    ServerAdmin me@my-email.com
    DocumentRoot /var/www/laravel2.tst/htdocs/public
    ErrorLog /var/www/laravel2.tst/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

I can confirm that the paths are correct (i put my dev projects in a htdocs folder and with laravel I extend this to the public folder)

*** EDIT2 *** Here is my web.php files contents

<?php

use App\Http\Controllers\AccountController;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

Route::middleware(['auth:sanctum', 'verified'])->get('/accounts/{id}/connect', [\App\Http\Controllers\AccountController::class, 'connect'] );

Route::middleware(['auth:sanctum', 'verified'])->resource('/accounts', \App\Http\Controllers\AccountController::class);

Route::middleware(['auth:sanctum', 'verified'])->get('/dashboard', function () {
    return view('dashboard');
})->name('dashboard');

All routes work correctly except i have to use index.php in the url (ie https://laravel2.tst/index.php or https://laravel2.tst/index.php/accounts/5/edit ) this is causing issues with links in the app in that i have to use /index.php/{rest of link}

Finally after struggling with this for 2 weeks with no help I found a solution to this which hopefully will help others facing this same scenario. Nothing of a mention of this in the docs and nobody here seemed to be able to help.

First I added an Alias in the apache conf file for this item

Alias /laravel2.tst /var/www/laravel2.tst/htdocs/public/

I then edited the .htaccess in the public folder by adding the following immediately after RewriteEngine On

RewriteBase /laravel2.tst

Then after restarting apache my urls are fixed and no longer require index.php to be in them to work. I'd say thanks for the help but...

Interestingly on the laravel 7 instance this wasn't needed and isn't configured. So no idea if this is actually needed, is the right way to do this or if there is another way which is better.

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