Welcome to Centmin Mod Community
Become a Member

Sysadmin Nginx redirect requests to non existent sub directories and files to the root dir

Discussion in 'System Administration' started by SFLC, Dec 15, 2016.

  1. SFLC

    SFLC Active Member

    223
    59
    28
    Dec 4, 2016
    The Canadas
    Ratings:
    +112
    Local Time:
    1:55 PM
    1
    10
    I want to redirect all requests for non-existent subdirectories and non-existent files and files within non-existent subdirectories back to the root page.

    Ex. sampledomain.com/jahsdjksf/sfhsafh.php or sampledomain.com/jahsdjksf/sdfsndf/sfhsafh.php or sampledomain.com/sfhsafh.php

    all back to sampledomain.com

    currently I have the following added to my rules

    root /home/nginx/domains/randwebsite.com/public/; index index.php;

    and

    try_files $uri $uri/ /index.php;

    but the issue is when I try any random non-existent sub dirs it loads the main page under that url and its broken as the css etc is using that url with the non-existent sub dir however only works perfectly with non-existent files with no sub dir added ex. sampledomain.com/sdlfsdlfsf.php

     
  2. eva2000

    eva2000 Administrator Staff Member

    54,895
    12,240
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,811
    Local Time:
    9:55 PM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
  3. SFLC

    SFLC Active Member

    223
    59
    28
    Dec 4, 2016
    The Canadas
    Ratings:
    +112
    Local Time:
    1:55 PM
    1
    10
    i tried that, i didnt want the error pages coming up so i changed the 404 to just / instead of /404...

    but it doesnt work, just brings up the main page and its broken, i would prefer if the person is fully redirected back to the main page