问题描述
所以,我知道这看起来可能有点怪,但我为统一起见,我想我所有的网址,以这种形式出现: HTTP: //domain.com/page/ 的到目前为止,我已经得到工作的常规页面,但我似乎无法得到错误页面正常工作。
So I know this may seem a little strange but I for sake of consistency, I would like all my urls to appear in this form: http://domain.com/page/ So far I have gotten the regular pages working but I cannot seem to get the error pages working properly.
如果用户访问一个网页或目录不存在,我想浏览器的硬盘重定向到: http://domain.com / 404 / 该目录,但实际上并不会存在。错误页面的实际位置将是下/pages/errors/404.php
If the user visits a page or directory that does not exist, I would like the browser to hard redirect to: http://domain.com/404/ This directory, however, will not actually exist. The real location of the error page will be under /pages/errors/404.php
另外,虽然我并不需要所有的各种错误(400,401,403,404,500)一个确切的答案,我会采用什么方法给重定向所有这些为他们的正确的网址(如: http://domain.com/400/ http://domain.com/500/ 等。)
Also, although I do not need an exact answer for all the various errors (400, 401, 403, 404, 500), I will be applying whatever method is given to redirect all of these to their "proper" URL's (eg. http://domain.com/400/ http://domain.com/500/ etc.)
任何想法?
推荐答案
尝试在你的.htaccess:
Try this in your .htaccess:
的.htaccess
ErrorDocument 404 http://example.com/404/
ErrorDocument 500 http://example.com/500/
# or map them to one error document:
# ErrorDocument 404 /pages/errors/error_redirect.php
# ErrorDocument 500 /pages/errors/error_redirect.php
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/404/$
RewriteRule ^(.*)$ /pages/errors/404.php [L]
RewriteCond %{REQUEST_URI} ^/500/$
RewriteRule ^(.*)$ /pages/errors/500.php [L]
# or map them to one error document:
#RewriteCond %{REQUEST_URI} ^/404/$ [OR]
#RewriteCond %{REQUEST_URI} ^/500/$
#RewriteRule ^(.*)$ /pages/errors/error_redirect.php [L]
在的ErrorDocument
重定向所有404到特定的URL,所有的500S到另一个URL(与您的域名替换)。
The ErrorDocument
redirects all 404s to a specific URL, all 500s to another url (replace with your domain).
重写规则的URL映射到您的实际404.php脚本。在常规的RewriteCond前pressions可如果你想更通用的,但我认为你必须明确地定义要覆盖所有的ErrorDocument codeS。
The Rewrite rules map that URL to your actual 404.php script. The RewriteCond regular expressions can be made more generic if you want, but I think you have to explicitly define all ErrorDocument codes you want to override.
本地重定向:
更改的.htaccess ErrorDocument来一个真实存在的文件(必须存在,否则你会得到一个错误):
Change .htaccess ErrorDocument to a file that exists (must exist, or you'll get an error):
ErrorDocument 404 /pages/errors/404_redirect.php
404_redirect.php
<?php
header('Location: /404/');
exit;
?>
基于错误号重定向
看起来你需要为你想要重定向(看到每一个错误指定的.htaccess的的ErrorDocument
行:和的)。在的.htaccess 上面的例子中有多个实例。您可以使用下面的通用重定向脚本来代替404_redirect.php以上。
Looks like you'll need to specify an ErrorDocument
line in .htaccess for every error you want to redirect (see: Apache ErrorDocument and Apache Custom Error). The .htaccess example above has multiple examples in it. You can use the following as the generic redirect script to replace 404_redirect.php above.
error_redirect.php
<?php
$error_url = $_SERVER["REDIRECT_STATUS"] . '/';
$error_path = $error_url . '.php';
if ( ! file_exists($error_path)) {
// this is the default error if a specific error page is not found
$error_url = '404/';
}
header('Location: ' . $error_url);
exit;
?>
这篇关于404重定向htaccess的错误后重写URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!