【问题标题】:php htaccess folders managementphp htaccess 文件夹管理
【发布时间】:2014-07-12 15:38:47
【问题描述】:

现在我有所有对我的域的请求都通过 /index.php

Options +FollowSymLinks
RewriteEngine on

RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.php [L]

但是我想添加一个例外。如果请求转到文件夹“/this_specific_folder/”,那么它应该转到“/this_specific_folder/index.php”而不是“/index.php”,而不会破坏当前现有的逻辑。

我怎样才能做到这一点?

【问题讨论】:

  • RewriteRule ^(/?this_specific_folder)/?$" $1/index.php [L] 如果它是真实路径,这应该可以工作。
  • 没用。 “/this_specific_folder”是一个真实的路径,但是如果我将其他文件夹(不是真实的)添加到路径“/this_specific_folder/this_not_real_subfolder”,它会被重定向到根目录“/index.php”
  • 更正问题。应该是 /this_specific_folder/non-real-path -> /this_specific_folder/index.php 和 /this_specific_folder/real-path -> /this_specific_folder/real-path ?
  • 在 /this_specific_folder 之后没有真正的子文件夹。应该可以做 /this_psecific_folder/path1/path2 这应该被重定向到 /this_specific_folder/index.php

标签: php regex apache .htaccess mod-rewrite


【解决方案1】:

你可以这样:

Options +FollowSymLinks
RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(this_specific_folder)(?:/.*)?$ /$1/index.php [L,NC]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [L,R=301]    

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.php [L]

【讨论】:

  • 与之前评论的行为相同。如果我将其他文件夹(不是真实的)添加到路径“/this_specific_folder/this_not_real_subfolder”,它会被重定向到根目录“/index.php”
  • 它给出 500 错误响应
  • 我认为它应该与 !-f 检查的最后一条规则重复,但在规则中使用前缀 ^this_specific_folder/
猜你喜欢
  • 2010-12-15
  • 1970-01-01
  • 2013-07-18
  • 1970-01-01
  • 1970-01-01
  • 2015-02-08
  • 2012-03-13
  • 2011-01-11
  • 1970-01-01
相关资源
最近更新 更多