【问题标题】:How to rewrite all 404's to index.php using cPanel URLs?如何使用 cPanel URL 将所有 404 重写为 index.php?
【发布时间】:2014-03-04 06:18:12
【问题描述】:

我正在尝试将所有 404 重写为 index.php,在其中我使用 PHP 的 parse_url() 来确定要包含的文件(例如 about-us.php、contact-us.php),我得到了一些非常奇怪的东西结果。

我正在处理由 cPanel 自动创建的“开发”URL: http://xxx.xxx.xxx.xxx/~mySite/

当前方法

我的.htaccess 文件包含以下内容:

RewriteEngine on
RewriteBase /~mySite
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-_.]+)$ index.php

结果好坏参半:

xxx.xxx.xxx.xxx/~mySite/contact-us 渲染得很好。

xxx.xxx.xxx.xxx/~mySite/contact-us/phone 抛出一个未被mod_rewrite 捕获的 404。

xxx.xxx.xxx.xxx/~mySite/about-us 重写到 server 根目录 (xxx.xxx.xxx.xxx/index.php)。

以前尝试过

ErrorDocument 404 /~mySite/index.php

我得到了类似的结果,除了以下几点:

xxx.xxx.xxx.xxx/~mySite/contact-us/phone 重写为 index.php,但我所有的 CSS 和 JS 包含已关闭,因为它们试图相对于 /~mySite/contact-us 而不是 /~mySite 加载。

有什么帮助吗?我快疯了。尤其是 contact-us 工作正常,但 about-us 不行?

【问题讨论】:

  • 为什么不只是ErrorDocument 404 /index.php
  • 嗨@MarcB,请参阅上面的新编辑。
  • 需要明确的是,ErrorDocument 404 /index.php 也不适用于字符串 about-us,原因不明。

标签: apache .htaccess url-rewriting http-status-code-404 cpanel


【解决方案1】:

首先不要使用 cPanel 预览。这不是查看您的开发网站的好方法。谁知道它将如何影响规则。无论如何,控制面板也会做一些奇怪的事情。

使用您的 real site 域名预览您的网站。您可以这样做,我在您的计算机上修改您的HOST 文件,以便只有您可以通过域名查看它。这个little guide 将向您展示如何编辑它。大概需要2分钟。

那么这应该有助于更好地检查事情。

错误文档不起作用的原因很可能是因为 ~mySite 很可能不是您的文档根目录。这通常是 cpanel 的预览链接方式。所以你真正的错误文件可能应该像 Marc B 所说的那样。

ErrorDocument 404 /index.php

如果您想要一个 mod_rewrite 解决方案,这也应该有效。但我不会同时使用。

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ / [R=301,L]

附带说明,我认为使用 404 页面通常很好,这样用户就知道请求的页面不是真实页面,而不是一些人认为您的网站有问题,因为它重定向到主页.就像facebook一样,它会给你一个大拇指,上面贴着绷带,说页面不可用。我见过很多非常聪明的自定义 404 页面,所以也许是时候发挥创意了。

【讨论】:

  • 优秀。像魅力一样工作。从现在开始,我将使用此方法而不是 cPanel 中的开发 URL。非常感谢,杰克。
猜你喜欢
  • 1970-01-01
  • 2013-06-25
  • 1970-01-01
  • 1970-01-01
  • 2019-01-29
  • 2022-08-19
  • 1970-01-01
  • 1970-01-01
  • 2015-01-10
相关资源
最近更新 更多