mod_rewrite: redirect top level directories to a file

Question!

I'd like to redirect all top level directories to a file using mod_rewrite.

So the following should redirect there:

- http://example.com/test
- http://example.com/test8/ 
- http://example.com/test_9231/

The following should NOT redirect there:

- http://example.com/test.php
- http://example.com/test_9231/test/
- http://example.com/test/test.php
- http://example.com/test_9231/test

None of the directories will physically exist. Directory names will only contain these characters: A-Za-z0-9_-

I tried this RewriteRule /(.*) /index.php [L] but subdirectories are still redirected.
I thought adding a slash after the 2nd parenthesis would do the job, but it just broke the redirecting.



Answers
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?[^/] /?$ /index.php [L]
By : Wrikken


RewriteRule ^/[A-Za-z0-9_-] /?$ /index.php

This will match a slash, then a name according to your spec, then another optional slash, then end of string, so subdirectories won't match

By : unbeli


This video can help you solving your question :)
By: admin