我想配置.htaccess文件來重新路由所有的URL到最近的指數子目錄,這樣的:使用。 htaccess的重寫的URL中的子目錄只
http://example.com/admin/blah/test/whatever應該留在地址欄,而是指向:
- http://example.com/admin/blah/test/whatever/index.php如果它存在,否則:
- http://example.com/admin/blah/test/index.php如果它存在,否則:
- http://example.com/admin/blah/index.php如果它存在,否則:
- http://example.com/admin/index.php
如果url是一個實際的文件,它應該總是去那個文件。所以http://example.com/admin/blah/file.css或http://example.com/admin/blah/item.inc都應該仍然工作,否則它應該重定向到該文件夾中的index.php或最近的父文件夾,如上所示。
我也想這隻影響一個子文件夾,如果這是可能的話。在上面的例子中,我假設.htaccess文件將進入/ admin /文件夾。
更新: 下面是當前工作:
# No Directory Listings or trailing slashes
Options -Multiviews -Indexes +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine On
DirectorySlash Off
# See if the current request is a directory containing an index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.php -f
RewriteRule ^(.*)/?$ $1/index.php [L,QSA]
# if index.php doesn't exist in current dir then
# forward to the parent directory of current REQUEST_URI
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1$2/index.php !-f
RewriteRule ^(.*?/)?([^/]+)/?$ $1 [L,QSA]
</IfModule>
這主要工作。如果輸入http://example.com/admin?p=2,則按預期解決問題,但如果使用網址http://example.com/admin/?p=2,則也可以解決此問題,而不會明確刪除尾部斜槓。
如果您需要子文件夾特定的規則,請將.htaccess文件放在子文件夾中。 –