【问题标题】:Apache 404 url not workingApache 404 网址不起作用
【发布时间】:2014-11-18 19:26:57
【问题描述】:

我的公司网站有一些问题。我的同事将.htaccess 文件留在了_control 目录中,如下所示:

# Virtual site directory
#
# This script lets us make use of default
# versions of files used in a typical site.
# When a request on the site is made for a file 
# that doesn't exist, instead of a 404 we rewrite
# the path to /_control/site/
#
# Any file in the directory this script is in will 
# take precedence over this script, so this script
# only comes into play if the file does not exist.

# First, set up URL rewriting
Options +FollowSymLinks
RewriteEngine On

# Add trailing slash for directories
# TODO: fix if site not in root dir (e.g. DEV server)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !.+\.(php|js|css|gif|jpg|jpeg|png)$ [NC]
RewriteCond %{REQUEST_URI} !.*/$
RewriteRule ^(.*)$ /$1/ [L,R=301]

# This Apache mod_rewrite rule checks to see
# if the requested file exists, if it doesn't
# then we rewrite to the respective site location.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !^.*/_control/(admin|common).*$
RewriteCond %{REQUEST_FILENAME} !^.*/_control/site/.*$
RewriteRule ^(.*)$ site/$1 [L]

我的应用程序的结构如下:

一切正常,但大约一周前,我们的 mysite.com/admin 和 404 页面开始重定向到托管公司的网站。 DaveG 已经在这里回答了问题的第一部分:https://stackoverflow.com/a/26013322/1933380,我能够让虚拟目录正常工作。我仍然遇到 404 页面的问题。我是否遗漏了什么或犯了错误?

【问题讨论】:

  • 我在您的 .htaccess 中没有看到任何 ErrorDocument 404 行。此外,您的规则是说任何不存在的文件或目录将其发送到site/$1,所以显然这会影响 404 处理。

标签: apache .htaccess


【解决方案1】:

您现在所做的是告诉网络服务器在另一个文件夹 (_control) 中查找所有不存在的页面和目录。这并没有为拼写错误、不存在的页面等提供解决方案。如果你想解决这个问题,你可以使用:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !^.*/_control/(admin|common).*$
RewriteCond %{REQUEST_FILENAME} !^.*/_control/site/.*$
RewriteRule ^(.*)$ site/errhandler.php?file=$1 [L]

并创建一个名为 errhandler.php 的文件,该文件显示自定义 404 页面,并且(例如)根据数据库搜索或其他内容提供可能的替代方案。然后您可以使用$_GET['file'] 来显示原始文件名。

【讨论】:

  • 它没有使用我创建的错误文件,所以我最终在我拥有的代码下面使用了这个代码:ErrorDocument 404 /site/error.php,它现在可以正常工作了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-17
  • 2015-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多