【问题标题】:Page not found should works with rewrite rule htacces找不到页面应该与 rewriterule htaccess 一起使用
【发布时间】:2012-07-09 18:51:21
【问题描述】:

我想将我的 url 从 php 扩展重写为 html.and 如果该文件不是 找到然后它应该重定向到一些客户页面(404.php)。

我写了这样的规则:

RewriteEngine on
RewriteRule ^$ front/index.php?query=$1 [NC,L,QSA]
RewriteRule (.*)\.(html) ?page=$1 [L,NC,QSA]
ErrorDocument    404    /expertLocal/?page=404

使用这个我面临这样的问题。 http://somedomain.com/expertLocal/contact.html

它将工作文件因为我有contact.php 页面。如果我会写 somedomain.com/expertLocal/contactxyz.php 然后它会重定向到 404.php 页面但是如果我会在地址栏中写http://somedomain.com/expertLocal/contactxyz.html 那么它会给出警告而不是 404.php 页面。

这是因为我使用了自己的框架,并且在这个结构中文件首先重定向到控制器索引文件,然后在其中包含请求的文件。

include(FRONT_CONTROLLER_PATH . $page . '.php');   like this.

你能帮帮我吗?

提前谢谢..

【问题讨论】:

  • 这里有联系人控制器,它正在处理contact.html 文件。对吧??发布您的警告信息。是 MVC 错误吗?

标签: .htaccess mod-rewrite errordocument


【解决方案1】:

我不确定你的框架,但如果有帮助,请检查下面的代码。

$filename = FRONT_CONTROLLER_PATH . $page . '.php';
if (file_exists($filename)) 
{
       include($filename); 
}
else
{
       // include 404 page here
}

【讨论】:

  • 您的项目中是否已有 404 页面? somedomain.com/expertLocal/404.html如果可以直接在浏览器中运行?
  • RewriteEngine on RewriteRule ^$ front/index.php?query=$1 [NC,L,QSA] RewriteRule (.*)\.(html) ?page=$1 [L,NC,QSA] ErrorDocument 404 /expertLocal/?page=404 无论我在页面名称中传递什么,它第一次转到 front/index.php 页面因为在项目中我们有那个文件,所以我认为使用 htaccess 规则我无法实现,所以最好使用 php 代码作为文件存在于控制器文件中。
猜你喜欢
  • 1970-01-01
  • 2015-11-16
  • 1970-01-01
  • 2011-12-05
  • 1970-01-01
  • 2013-02-17
  • 2015-02-24
  • 1970-01-01
  • 2012-12-10
相关资源
最近更新 更多