routing

Discussion in 'Programming/Scripts' started by Kemp, Apr 17, 2026.

  1. Kemp

    Kemp Member

    Hi,

    I have setup a routing system using php and nginx, as follows:

    index.php

    <?php

    $request = $_SERVER['REQUEST_URI'];

    switch ($request) {

    case '':
    case '/':
    require __DIR__ . '/directory1/index.php';
    break;

    case '/view/one':
    require __DIR__ . '/directory2/index.php';
    break;

    }

    --

    I have added the following to nginx directives in ISPConfig:

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

    --

    When I go to domain.com/view/one it's loading the /directory1/index.php file instead of /directory2/index.php

    Thank you!
     
  2. pyte

    pyte Well-Known Member HowtoForge Supporter

    Wouldn't this be much easier?`

    Code:
    location = / {
        rewrite ^ /directory1/index.php last;
    }
    
    location = /view/one {
        rewrite ^ /directory2/index.php last;
    }
    
    location / {
        try_files $uri $uri/ =404;
    }
     
  3. ahegarosib97

    ahegarosib97 New Member

    $_SERVER['REQUEST_URI'] may include the query string, so try checking what it actually contains with var_dump($request). Also make sure /view/one isn't being matched by an existing directory. Using parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) is usually a more reliable way to route requests.
     
  4. ahegarosib97

    ahegarosib97 New Member

    Hope it will help you
     

Share This Page