1

我问了一个[问题]:htaccess reverse directory here关于反向路由。但是,我不断获取目录视图而不是有问题的文件。

例如:我去 /img/header.jpg 我得到文件夹 /img/ 的内容,而文件 header.jpg 存在。我在选项中添加了 -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 个参数?

4

1 回答 1

0

将您的代码更改为:

选项 +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]
于 2012-05-08T12:12:35.793 回答