我问了一个[问题]:htaccess反向目录在这里关于反向路由。但是,我一直获得目录视图而不是有问题的文件。
例如:我转到/img/header.jpg我获取文件夹/img/的内容,而文件头.jpg存在。我在选项中添加了 -Indexes,但这只会导致 403 禁止访问消息。
我应该如何编辑我的htaccess以显示img/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]
提前致谢
编辑
我尝试在重写库/之后直接添加以下行:
RewriteCond %{REQUEST_FILENAME} !-f
这适用于大多数文件。只有当它是images/css/js/ico/etc时,这才应该有效。我认为目前如果它直接找到一个 php 文件,它会起作用。
我似乎无法弄清楚如何获取剩余的参数。
/index/foo/bar/for/
应该找到的文件是 foo ,如何获取 $_GET 中剩余的 2 个参数?
将代码更改为:
选项 +关注符号链接 -多视图 -索引
# 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]