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

b166er's avatar

route catch-all with path and non-existing php-files

hi,

i would like to catch all traffic and route them to my Controller. i found a way how to catch up path with to tenth depth, but i cannot find a solution to catch a request-url with non-existing php-file. here my route/web.php

<?php

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

Route::any('/{first?}/{second?}/{third?}/{fourth?}/{fifth?}/{sixth?}/{seventh?}/{eighth?}/{ninth?}/{tenth?}',
        [Controller::class, 'index']
)->name('catch_path');

Route::any('{catchall?}', [Controller::class, 'index'])->where('catchall', '.*');

my app/Http/Controllers/Controller.php

<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class Controller extends BaseController {
    use AuthorizesRequests,
        DispatchesJobs,
        ValidatesRequests;

    // Controller method definition...
    public function index(Request $request, ...$params) {
        return response('found');
    }

    /* 
      * magically gets called by laravel
      * source: https://stackoverflow.com/questions/34831175/how-do-i-make-a-catch-all-route-in-laravel/34831176#34831176
    */
    public function missingMethod(Request $request, $parameters = array()) {
        return $this->show($request, $params);
    }

    /**
     * Execute an action on the controller.
     *
     * @param  string  $method
     * @param  array  $parameters
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function callAction($method, $parameters) {

        if (true === method_exists($this, $method)) {
            $output = parent::callAction($method, $parameters);
        } else {
            $request = request();
            $this->missingMethod($request, ['method' => $method,
                'parameters' => $parameters]);
        }
        return $output;
    }

    public function show(Request $request, $params = []) {
        Storage::append('unknown.debug.log', time() . ' - ' .
                var_export(['params' => $params, 'request' => $request], true));

        $content = time() . ' - ' .
                $request->getHost() . ' - ' .
                $request->path() . ' - ' .
                http_build_query($request->all())
        ;
        Storage::append('unknown.log', $content);
        dd('not defined::', $request, $params);
        return response('not defined');
    }

}

my nginx-vhost-conf:

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name .test.com;
    root   /var/www/php/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.html index.htm index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;


    location ~ /\.(?!well-known).* {
        deny all;
    }

    ssl_certificate     /etc/nginx/certs/$host-bundle.cert.pem;
    ssl_certificate_key /etc/nginx/certs/$host.key.pem;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_prefer_server_ciphers on;
    ssl_dhparam /etc/nginx/certs/dhparam.pem;
    ssl_ciphers HIGH:!aNULL:!MD5:!RC4;



    location ~ \.php$ {
        fastcgi_index index.php;
        
        fastcgi_param  QUERY_STRING       $query_string;
        fastcgi_param  REQUEST_METHOD     $request_method;
        fastcgi_param  CONTENT_TYPE       $content_type;
        fastcgi_param  CONTENT_LENGTH     $content_length;
        
        fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
        fastcgi_param  REQUEST_URI        $request_uri;
        fastcgi_param  DOCUMENT_URI       $document_uri;
        fastcgi_param  DOCUMENT_ROOT      $document_root;
        fastcgi_param  SERVER_PROTOCOL    $server_protocol;
        fastcgi_param  REQUEST_SCHEME     $scheme;
        fastcgi_param  HTTPS              $https if_not_empty;
        
        fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
        fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;
        
        fastcgi_param  REMOTE_ADDR        $remote_addr;
        fastcgi_param  REMOTE_PORT        $remote_port;
        fastcgi_param  SERVER_ADDR        $server_addr;
        fastcgi_param  SERVER_PORT        $server_port;
        fastcgi_param  SERVER_NAME        $server_name;
        
        # PHP only, required if PHP was built with --enable-force-cgi-redirect
        fastcgi_param  REDIRECT_STATUS    200;
        
        #fastcgi_param  SCRIPT_FILENAME    $realpath_root$fastcgi_script_name;
        fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;

        fastcgi_pass  backend:9000;
    }
}
server {
    listen      80;
    listen      [::]:80;
    server_name .test.com;
    return      301 https://$host$request_uri;
}

this URL works and wil catched by my routes:

but this URL i can not catch with my route:

error on nginx:

[error] 32#32: *1302 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: x.x.x.x, server: test.com, request: "GET /62/870/multi_tracker.php?...

i tried already to copy /public/index.php to /public/multi_tracker.php but without success. how can i catch this url with a route? any ideas? thanks!

0 likes
1 reply
b166er's avatar
b166er
OP
Best Answer
Level 1

ok, i think i found a solution, it was my nginx.conf, here my new modfied nginx-config:

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name .test.com;
    root   /var/www/php/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.html index.htm index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;


    location ~ /\.(?!well-known).* {
        deny all;
    }

    ssl_certificate     /etc/nginx/certs/$host-bundle.cert.pem;
    ssl_certificate_key /etc/nginx/certs/$host.key.pem;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_prefer_server_ciphers on;
    ssl_dhparam /etc/nginx/certs/dhparam.pem;
    ssl_ciphers HIGH:!aNULL:!MD5:!RC4;



    location ~ \.php$ {
        try_files $uri @missing; # <-- this is NEW!

        include fastcgi_params;  # moved to separate file
        fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;

        fastcgi_pass  backend:9000;
    }

# this block catch all files!
    location @missing {
        rewrite ^ /error/404 break;

        fastcgi_pass backend:9000;
        fastcgi_param SCRIPT_FILENAME $document_root/index.php;
        include fastcgi_params;
    }
}
server {
    listen      80;
    listen      [::]:80;
    server_name .test.com;
    return      301 https://$host$request_uri;
}

i found this solution here: https://serverfault.com/a/219081/648090

Please or to participate in this conversation.