【问题标题】:redirect subfolder/index.php to subfolder/将子文件夹/index.php 重定向到子文件夹/
【发布时间】: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


【解决方案1】:

注释掉这条规则:

RewriteRule ^index\.php$ / [NC,R=301,L]

并在同一个地方插入这条规则:

RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteCond %{REQUEST_URI} ^(.*/)index\.php$ [NC]
RewriteRule ^ %1 [L,R=301,NE]

【讨论】:

  • 优秀 - 这完美。非常感谢您的帮助!
  • anubhava - 你知道我如何排除一个特定的 URL,例如 example.com/blah/index.php,这样它就不会被任何 URL 重写?
  • 跳过在RewriteEngine下面添加RewriteRule ^blah/index\.php$ - [L,NC]
猜你喜欢
  • 2016-05-08
  • 2012-10-23
  • 2019-04-29
  • 1970-01-01
  • 1970-01-01
  • 2021-09-08
  • 2017-12-05
  • 2013-10-26
  • 1970-01-01
相关资源
最近更新 更多