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

goheels's avatar

Custom script in public folder

If I post the code below in /var/www/laravel/public/test_folder and I visit test.com/test_folder

I get: noSession ID: 4ehbvd607lcjb6bkv9qs9tf3t7 Auth Check: no User: not logged in

but if I go to test.com/test_folder/index.php it works. Seems like it could be an nginx issue, but I can't figure that out either =[

require __DIR__.'/../../vendor/autoload.php';
$app = require_once __DIR__.'/../../bootstrap/app.php';

// Bootstrap the application
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
$response = $kernel->handle(
    $request = Illuminate\Http\Request::capture()
);

use Illuminate\Support\Facades\Auth;

// Debug information
echo "Session ID: " . session_id() . "<br>";
echo "Auth Check: " . (Auth::check() ? 'yes' : 'no') . "<br>";
echo "User: " . (Auth::user() ? Auth::user()->name : 'not logged in') . "<br>";

// Clean up
$kernel->terminate($request, $response);

I've also tried a custom route.

Route::any('test_folder_route/{path?}', function($path = null) {
    return require_once(public_path('test_folder/index.php'));
})->withoutMiddleware([])
->where('path', '.*');

Any help or advice is appreciated. I usually just use views, but I can't for this at this moment. I feel like I've tried all that I can. If I can't get it.. I reckon I'll juss rock the index.php like im in 1999.

0 likes
2 replies
LaryAI's avatar
Level 58

The issue you're experiencing is likely related to how Nginx is configured to handle requests to directories without a trailing slash. When you visit test.com/test_folder, Nginx might be treating it as a directory request and not automatically appending /index.php to the URL, which is why it works when you explicitly visit test.com/test_folder/index.php.

To resolve this, you can adjust your Nginx configuration to ensure that requests to directories automatically append /index.php if it exists. Here's how you can modify your Nginx configuration:

  1. Open your Nginx configuration file. This is usually located at /etc/nginx/sites-available/default or a similar path depending on your server setup.

  2. Find the server block handling your domain and add the following configuration:

location /test_folder {
    index index.php;
    try_files $uri $uri/ /test_folder/index.php?$query_string;
}
  1. Save the changes and restart Nginx to apply the new configuration:
sudo systemctl restart nginx

This configuration does the following:

  • Sets index.php as the default file to serve when a directory is requested.
  • Uses try_files to attempt to serve the requested URI, then the URI with a trailing slash, and finally falls back to index.php with any query string parameters.

By making these changes, visiting test.com/test_folder should now correctly serve index.php without needing to explicitly include it in the URL.

goheels's avatar

@LaryAI nginx route appears but doesnt work with Auth. even if I hardcode these

require '/var/www/laravel/vendor/autoload.php';
$app = require_once '/var/www/laravel/bootstrap/app.php';

Please or to participate in this conversation.