【问题标题】:mod_rewrite redirect if file not found如果找不到文件,则 mod_rewrite 重定向
【发布时间】:2011-08-02 17:52:38
【问题描述】:

我有一个网站,它使用前端控制器将流量重定向到某些文件,因此我可以使用 SEO 友好的 URL,但是网站上有一些静态页面,我不希望流量被引导离开:

我目前有以下 .htaccess:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.exmple.com/$1 [L,R=301]
ErrorDocument  404  /controller.php

这似乎工作正常:

  • 所有流量都转向www.example.com;
  • 并且任何没有找到的网页都被定向到controller.php处理;
  • 任何实际存在的东西,例如 www.example.com/about_us.php 仍然有效;

问题在于,即使页面仍然发送并正确显示,它仍然报告 404 错误 - 它看起来对最终用户没有任何不同,但是当 Google 机器人看到 404 时,它会扰乱我的 SEO并假设那里没有其他东西。

有什么方法可以简单地重定向除某些页面列表之外的所有流量,还是有更优雅的解决方案?

欢迎任何帮助、想法、cmets 等

【问题讨论】:

  • 使用你现在所拥有的——当你使用header() PHP 函数成功路由请求时,你是否尝试过发送 200 响应代码?

标签: php .htaccess mod-rewrite url-rewriting front-controller


【解决方案1】:
RewriteEngine On
ErrorDocument  404  /controller.php

RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,NC]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule .* index.php/$0 [PT]

你应该通过 index.php... 否则下面的行应该工作而不是上面的行。

RewriteRule .* $0 [PT]

【讨论】:

  • 考虑到 OP 已经在使用/controller.php 来处理对不存在资源的所有请求,不需要额外的index.php -- 最后一行可以很容易地替换为:@987654325 @
猜你喜欢
  • 1970-01-01
  • 2014-02-26
  • 1970-01-01
  • 2012-10-07
  • 2018-03-20
  • 2020-05-20
  • 1970-01-01
  • 1970-01-01
  • 2012-10-09
相关资源
最近更新 更多