From the following url:
localhost/videos/the-best-games
and a directory named "categories" with various files in it located in the same directory as .htaccess :
./games.txt
./sports.txt
./football.txt
How to use the url slug "the-best-games" to check these filenames in "categories"
./categories/the.txt
./categories/best.txt
./categories/games.txt
And rewrite the url using the first matching filename:
localhost/games/videos/the-best-games
OR
localhost/games/videos/the-best
Maybe based on these previous questions:
This should work for you:
RewriteCond /path/to/categories/$1.txt -f
RewriteRule /videos/the-best-(.*)$ /$1/videos/the-best-$1 [R,L]
It will parse the rule and look for the-best-(whatever), take that value and examine the file system for the file at the absolute path to your categories. Then it will do the rewrite. If you want to remove the category from the end of the rewritten URL:
RewriteCond /path/to/categories/$1.txt -f
RewriteRule /videos/the-best-(.*)$ /$1/videos/the-best [R,L]