Htaccess permission denied / Directory index shown

混江龙づ霸主 提交于 2019-12-13 00:12:15

问题


I asked a [question]: htaccess reverse directory here about reverse routing. However I keep getting the directory view instead of the file in question.

For example: I go to /img/header.jpg I get the content of the folder /img/ while the file header.jpg exists. I added -Indexes in the options but that just results into a 403 forbidden access message.

How should I edit my htaccess to show imgs/js/css etc but still keep the recursive structure?

current 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]

thanks in advance

Edit

I have tried adding the following line directly after RewriteBase /:

RewriteCond %{REQUEST_FILENAME} !-f

This work for most files. Only this should only work when it are images/css/js/ico/etc. I think at the moment if it finds a php file directly it would work to.

What I cant seem to figure out is how to get the remaining parameters.

 /index/foo/bar/for/

File that should be found is foo , how do I get the 2 remaining parameters in a $_GET?


回答1:


Change your code to:

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]

EDIT: Based on OP's comments this solution fills a query parameter param with the remainder of a path:

# 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]


来源:https://stackoverflow.com/questions/10496267/htaccess-permission-denied-directory-index-shown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!