开发者

mod_rewrite: redirect top level directories to a file

开发者 https://www.devze.com 2023-01-07 05:11 出处:网络
I\'d like to redirect all top level directories to a file using mod_rewrite. So the following should redirect there:

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 red开发者_Python百科irected.

I thought adding a slash after the 2nd parenthesis would do the job, but it just broke the redirecting.


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


RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?[^/]+/?$ /index.php [L]
0

精彩评论

暂无评论...
验证码 换一张
取 消