dousi4148
2018-04-20 07:26
浏览 97
已采纳

将子文件夹/ index.php重定向到子文件夹/

I'm trying to redirect any requests to subfolder/index.php to subfolder/ and still display the contents of the subfolder's index.php file. This isn't specific to just one subfolder so I need a general rule. But note that some directories, such as css/ and images/ won't have an index.php file and I'd just want to deny access to them if the user tried to hit those specific directories.

At the moment, a request to subfolder/index.php goes to subfolder/index/ (but does still actually show the contents of index.php). This is due to the rule I have to replace .php extensions with a trailing slash (thanks to the other posts on this forum!)

So I think I'm just missing a rule that sits above my trailing slash rule.

So far I have this:

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]

Any help would be much appreciated as I've been struggling with the htaccess file for a while and can't work out the answer.

图片转代码服务由CSDN问答提供 功能建议

我正在尝试将任何请求重定向到子文件夹/ index.php </ code>到子文件夹/ </ code>并仍然显示子文件夹的 index.php </ code>文件的内容。 这不仅仅针对一个子文件夹,因此我需要一般规则。 但请注意,某些目录,例如 css / </ code>和 images / </ code>将没有index.php文件,我只想拒绝访问它们,如果用户 试图点击那些特定目录。</ p>

目前,对子文件夹/ index.php </ code>的请求转到子文件夹/ index / </ code> (但实际上仍显示index.php的内容)。 这是因为规则我必须用尾随斜杠替换.php扩展名(感谢此论坛上的其他帖子!)</ p>

所以我想我只是错过了一条规则 坐在我的尾随斜线规则之上。 </ p>

到目前为止我有这个:</ p>

  RewriteEngine On 
 
#make对域根的所有请求都显示索引的内容。  php 
DirectoryIndex index.php 
#永久性地将请求转移到index.php到域root 
RewriteRule ^ index \ .php $ / [NC,R = 301,L] 
 
#不允许目录列表\  nOptions -Indexes 
 
 
 ## **** INSERT RULE在这里处理子文件索引文件**** 
 
 
 ## hide .php extension snippet 
 
#到外部重定向/  dir / foo.php到/ dir / foo 
RewriteCond%{THE_REQUEST} ^ [AZ] {3,} \ s([^。] +)\ .php [NC] 
RewriteRule ^%1 / [R = 301,  L] 
 
#添加一个尾部斜杠
RewriteCond%{REQUEST_FILENAME}!-f 
RewriteCond%{REQUEST_URI}!/ $ 
RewriteRule。  %{REQUEST_URI} / [L,R = 301] 
 
#在内部转发/ dir / foo到/dir/foo.php
RewwriteCond%{REQUEST_FILENAME}!-d 
RewriteCond%{REQUEST_FILENAME} .php -f  
RewriteRule ^(。*?)/?$ $ 1.php [L] 
 </ code> </ pre> 
 
 

任何帮助都会非常感激,因为我一直在努力使用htaccess文件 一段时间后,无法解决问题。</ p> </ div>

1条回答 默认 最新

相关推荐 更多相似问题