我问了一个[问题]:htaccess reverse directory这里有关反向路由的问题。但是我继续获取目录视图而不是相关文件。
例如:我去/img/header.jpg文件header.jpg存在的时候我得到了文件夹/ img /的内容。我在选项中添加了-Indexes,但这只会导致403禁止访问消息。
我应该如何编辑我的htaccess以显示imgs / js / css等但仍保留递归结构?
目前的htacces:
Options +FollowSymLinks -MultiViews -Indexes
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{DOCUMENT_ROOT}/$1/$2.php !-f
RewriteRule ^(.*?)/([^/]+)/?$ $1/ [L]
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
提前致谢
修改
我尝试在RewriteBase /后直接添加以下行:
RewriteCond %{REQUEST_FILENAME} !-f
这适用于大多数文件。只有当它是images / css / js / ico / etc时才能使用它。我认为目前如果它直接找到一个php文件就可以了。
我似乎无法弄清楚如何获得剩余的参数。
/index/foo/bar/for/
应该找到的文件是foo,如何在$ _GET中获取剩余的2个参数?
答案 0 :(得分:0)
将您的代码更改为:
Options + FollowSymLinks -MultiViews -Indexes
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
# If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
# don't do anything
RewriteRule ^ - [L]
# if current ${REQUEST_URI}.php is not a file then
# forward to the parent directory of crrent REQUEST_URI
RewriteCond %{DOCUMENT_ROOT}/$1/$2.php !-f
RewriteRule ^(.*?)/([^/]+)/?$ $1/ [L]
# if current ${REQUEST_URI}.php is a valid file then
# load it be removing optional trailing slash
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
编辑:根据OP的评论,此解决方案使用路径的其余部分填充查询参数param
:
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
# If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
# don't do anything
RewriteRule ^ - [L]
# if current ${REQUEST_URI}.php is not a file then
# forward to the parent directory of crrent REQUEST_URI
RewriteCond %{DOCUMENT_ROOT}/$1/$2.php !-f
RewriteCond %{QUERY_STRING} ^(?:param=)?(.*)$
RewriteRule ^(.*?)/([^/]+)/?$ $1/?param=$2/%1 [L]
# if current ${REQUEST_URI}.php is a valid file then
# load it be removing optional trailing slash
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?)/?$ $1.php [L]