【问题标题】:php mod rewrite .htaccess ignore directoryphp mod rewrite .htaccess 忽略目录
【发布时间】:2013-01-23 19:52:35
【问题描述】:

我应该设置什么规则,让 mod_rewrite 完全忽略目录“public”? 我的意思是,文件应该可以在其中访问,但如果文件不存在,服务器错误页面应该会出现类似“禁止”或“未找到文件”之类的信息。我不需要自定义错误页面或类似的东西。我只是想让“公众”表现得像根本没有 mod_rewrite 一样。

这是我的 .htaccess 文件:

RewriteEngine on
RewriteCond $1 !^(index\.php|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

我的文件结构是

/system/
/application/
/public/

我希望文件夹 public 的行为,就像根本没有设置重写规则一样,完全忽略它。

编辑

这是我的 .htaccess 文件:

RewriteEngine on

RewriteRule ^(public)($|/) - [L,NC]

RewriteCond $1 !^(index\.php|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

我已经在 /public/ 文件夹中有这个 .htaccess:

RewriteEngine off

我已经尝试了上述所有不同的答案(以及来自谷歌的大量答案)。我试着把它们混在一起。

我的文件夹:

/system/
/application/
/public/ 
/public/.htaccess #RewriteEngine off
/public/favicon.ico
/index.php

下面是我得到结果的网址:

/public/favicon.ico -> I get the favicon
/public/faviDon.ico -> I get the index.php (without mod rewrite you would get "not found")
/public/ -> I get the index.php (without mod rewrite "forbidden")

因此,如果找不到文件或直接访问文件夹,它仍然会重写 url。

你能看吗?

非常感谢你们的努力!对此,我真的非常感激!

【问题讨论】:

  • 更适合 serverfault.com
  • 如果“public”是网络根目录,那么就您网站的网址而言,它会被“忽略”。

标签: php .htaccess mod-rewrite


【解决方案1】:

编辑

我在我的机器上完全设置了你的文件

//  /.htaccess

RewriteEngine on

RewriteRule ^(public)($|/) - [L,NC]

RewriteCond $1 !^(index\.php|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

.htaccess 在公用文件夹中:

//  /public/.htaccess

Options -Indexes
RewriteEngine off

这会禁用你想要的重写。

/public/                 -> 403 Forbidden
/public/favicon.ico      -> 200 File found
/public/not-existing.ext -> 404 File not found

您的公共文件夹中有 index.php 吗? 也许你可以删除那个..

您在哪种机器上进行测试?

我在 Linux + Apache 2 + PHP5.3 上测试过

我可以在下午(我的时间 +2 GMT)给你更多支持

编辑 2

当我从 /.htaccess 中删除此行时仍然有效

RewriteRule ^(public)($|/) - [L,NC]

一切都由公用文件夹中的 .htaccess 处理。

可能是您浏览器的缓存问题。尝试不同的浏览器/清理历史记录/安装应用程序以删除缓存..(取决于您使用的浏览器)

【讨论】:

  • 一切正常,例如:访问文件夹 public/ 重定向到 index.php(而不是 FORBIDDEN)。访问目录中不存在的文件会重定向到 index.php(而不是 FILE NOT FOUND)
  • 我快疯了 :(
  • +1 as 是比我的更好的解决方案。 @maximkott 你在/public/ 中有 index.php 吗?它是否重定向到根 index.php?如果是这样,请向我展示您的 .htaccess 文件以及 Ferry 的答案
  • 我已经按照你说的做了……对我没用。公共目录仍在重写中。我对您的解决方案进行了一些尝试,并发布了反馈!
  • 到目前为止运气不佳...我走了,将在 18:00 GMT+1 左右回来。所以不能回答你的帖子,只是你知道。祝你有美好的一天))
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-21
相关资源
最近更新 更多