Search code examples
php.htaccesstrailing-slash

Redirect if using trailing slash in URL. htaccess


How can I make it so when a user uses a trailing slash, for example this link:

website.com/staff/ 

It will redirect to a 404 not found page?


Solution

  • RewriteEngine on
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule /$ - [R=404,L]
    

    This will return a 404 not found error for all requests (except directories) if they contain a trailing slash.