DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Find Files In Previous Folders With Mod_rewrite

  • submit to reddit
        It recursively goes up in the directories structure until it finds the requested file name or fails with status 404

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
	RewriteRule ^[^/]*/(.*) $1

Assuming the file "/test/x.txt" exists, when requesting the url "/test/blabla/folder/xyz/x.txt", it will go up until finding the requested file:
/test/x.txt (found...)