【发布时间】: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