Want to subscribe to topics you're interested in?
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

    224
    59
    28
    Dec 4, 2016
    The Canadas
    Ratings:
    +112
    Local Time:
    7:32 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

    28,942
    6,570
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +9,750
    Local Time:
    3:32 AM
    Nginx 1.13.x
    MariaDB 5.5
  3. SFLC

    SFLC Active Member

    224
    59
    28
    Dec 4, 2016
    The Canadas
    Ratings:
    +112
    Local Time:
    7:32 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