Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Garbare's avatar

URLs only work when '/index.php/' is used

I have just launched a Laravel 5 project on a live server and all is going well except for one thing.

I need to write /index.php/ in the url to access any page other than the home page.

For example

sitename/                   -- this works fine

sitename/index.php          -- this also works fine

sitename/about              -- This causes a server 404

sitename/index.php/about        -- this works fine

I suspect this is an issue with the .htaccess file but I'm a little out of my comfort zone in this area.

Here is the .htacccess I'm using.

<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]
</IfModule>

This is the .htaccess that came with the install. I haven't changed anything. (Any changes have been reverted.)

Does anyone have any insight into this?

0 likes
29 replies
pmall's avatar

Does your vhost has the public folder as root dir ? Does it allows for .htaccess file ?

Also maybe you havn't mod rewrite enabled

1 like
Garbare's avatar

@pmall No, there is a public_html in the root folder. I have placed all of the laravel files in the root and put the contents of the laravel public folder into the publicly accessible public_html folder. (I should have mentioned my folder structure in the original post).

I have been told it does allow for .htaccess and that mod_rewrite is enabled. I will double check these last two thing and get back to you.

pmall's avatar

You have to configure things so it works with public_html instead of public, unfortunately I dont remember what

1 like
bashy's avatar

That is how Apache is passing the requests to PHP. Can you show how you've set that up?

Garbare's avatar

@bashy Sure I can show you but I don't follow exactly what you are asking for. Are you looking for the Apache config?

Prullenbak's avatar

I had to deploy some of our webapps to servers that we don't manage ourselves...That's when I come across problems like this. If the document root is set to public_html I usually do one of these two things:

If I can, I change the document root :)

If I can't do that, I delete the public_html directory and make a symbolic link to the public folder, named public_html. This usually works just fine.

Garbare's avatar

Just to be a bit clearer, the 404 pages that sitename/about is getting is generated by the server.

I have created custom 404 page complete with funny image and stuff. Everything is working, including my witty 404, but only if I include index.php in the URL like so sitename/index.php/about.

I suspect the 404 is triggered before it even gets to Laravel. I also have error reporting turned on which is working as it gave me an error about Fileinfo not been enabled while editing images. I am not getting any errors with sitename/about, just a server 404.

Garbare's avatar

Here’s what I have tried.

Edited public/index.php to include

/*
|--------------------------------------------------------------------------
| Turn On The Lights
|--------------------------------------------------------------------------
|
| We need to illuminate PHP development, so let us turn on the lights.
| This bootstraps the framework and gets it ready for use, then it
| will load up this application so that we can run it and send
| the responses back to the browser and delight our users.
|
*/

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

// set the public path to this directory
$app->bind('path.public', function() {
    return __DIR__;
});

As discussed here.

This didn't work.

Next I tried adding this to AppServiceProvider.php.

public function register()
{
    $this->app->bind('path.public', function() {
      return base_path().'/public_html';
    });
}

Also, discussed here.

This didn't work either.

Next I tried what @Prullenbak suggested.

I renamed the public_html to public and updated Apache’s httpd.conf accordingly. I restarted the server and ... still the same issue.

Thanks for all the help guys but nothing has worked yet.

bashy's avatar

Basically Apache reads parts after the URI segment and passes it to PHP. If you don't understand how Apache processes requests and passes them to PHP, please check that first.

skliche's avatar

@Garbare Try the following .htaccess. I never had any trouble with that on a server or localhost environment.

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]

</IfModule>
13 likes
rider0211's avatar

@skliche Hi how are you? I have been trying to find the way to fix this problem. Finally I found your solution. But the iis can't analyze your .htaccess content.

RewriteRule ^(.) - [E=BASE:%1] RewriteRule ^index.php(/(.)|$) %{ENV:BASE}/$2 [R=301,L]

There appear error in those lines. E and ENV are not supported things the iis say. Please help me.

Snapey's avatar

@rider0211 Start your own question. This one was asked 7 years ago and the people in it might have even retired or moved on to lesser languages.

Garbare's avatar

@skliche That did it! Problem solved. It was the .htaccess all along.

Thank you so much, I really appreciate your help.

2 likes
JoaoSantos's avatar

I have a similar structure, I have a folder laravel with the laravel files inside, and in the same level as laravel a public_html folder with a site folder and inside the contents of "Laravel/public".

What I had to do was changing index.php paths to reflect the new ones. And then I needed to change the .htaccess file. I tried more than 5 different configurations without success, but fortunately @skilche answer solved the problem! Thanks for the help and I think it sould be marked as the correct answer.

cpatenta's avatar

Had the same problem on Ubuntu Server 16.04. Solved it by setting AllowOverride to All and enabling mod rewrite

sudo a2enmod rewrite
sudo service apache2 restart

Hope this helps

4 likes
joshymatheww's avatar

I had the same issue. I am using ubuntu 16.04 as my local development environment. I have changed the apache.conf file /etc/apache2 from

<Directory /var/www/> Options Indexes FollowSymLinks AllowOverride None Require all granted

to

<Directory /var/www/> Options Indexes FollowSymLinks AllowOverride All Require all granted

and

<Directory /usr/share> AllowOverride None Require all granted

to

<Directory /usr/share> AllowOverride All Require all granted

After that it works perfectly. Now I can access the page without using index.php in the URL

After that you need to restart the apache using sudo service apapche2 restart command in the terminal

4 likes
MinZaw's avatar

thank you @cpatenta, i passed my error, after i run "sudo a2enmode rewrite" and "sudo service apache2 restart"

Lauwersoft's avatar

@joshymatheww Yup, this fixed it for me. However, i tested it without <Directory /usr/share> AllowOverride All Require all granted

And that worked as well. The problem lies with: <Directory /var/www/> Options Indexes FollowSymLinks AllowOverride None Require all granted

pubsajib's avatar

Yes solved by editing /etc/apache2/apache2.conf on my ubuntu 18.04

markuskoehler's avatar

@SKLICHE - @skliche out of curiosity, could you please explain what are the technical differences between the .htaccess you posted and the laravel default .htaccess?

Thanks in advance!

bsesic's avatar

I fixed the issue in Laravel 5.7 with that code in the .htaccess. The issue would give me an Error 500.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
ChappieZ's avatar

I was error like that but i solved like this.

I just move public/.htaccess to .htaccess.

jove's avatar

@chappiez hope you are not serving the project from the root of the project?

Please or to participate in this conversation.