【发布时间】:2018-09-30 20:30:56
【问题描述】:
我正在尝试将任何对subfolder/index.php 的请求重定向到subfolder/,并且仍然显示子文件夹的index.php 文件的内容。这不仅仅针对一个子文件夹,因此我需要一个通用规则。但请注意,某些目录,例如 css/ 和 images/ 不会有 index.php 文件,如果用户试图点击这些特定目录,我只想拒绝访问它们。
目前,对subfolder/index.php 的请求转到subfolder/index/(但实际上仍显示 index.php 的内容)。这是由于我必须用斜杠替换 .php 扩展名的规则(感谢本论坛上的其他帖子!)
所以我认为我只是错过了位于我的尾部斜杠规则之上的规则。
到目前为止,我有这个:
RewriteEngine On
# make all requests to the domain root show contents of index.php
DirectoryIndex index.php
#Permanently move requests to index.php to the domain root
RewriteRule ^index\.php$ / [NC,R=301,L]
#do not allow a directory listing
Options -Indexes
##**** INSERT RULE HERE TO DEAL WITH SUBFOLDER INDEX FILES****
## hide .php extension snippet
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R=301,L]
# add a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [L,R=301]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
任何帮助将不胜感激,因为我一直在努力处理 htaccess 文件并且无法找到答案。
【问题讨论】:
-
在
Options -Indexes之后添加这个:RewriteRule ^subfolder/$ subfolder/index.php [L] -
感谢您的快速回复。我已经添加了测试这条线,但它似乎没有做任何事情。你能解释一下它应该做什么吗?另请注意,我正在尝试为所有子文件夹实现这一点,而不是特定的子文件夹。谢谢。
标签: php apache .htaccess url-rewriting