重定向 404 错误 htaccess 后重写 URL [英] Rewrite URL after redirecting 404 error htaccess

查看:35
本文介绍了重定向 404 错误 htaccess 后重写 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我知道这可能看起来有点奇怪,但为了保持一致性,我希望我的所有网址都以这种形式出现: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)的确切答案,但我将应用所提供的任何方法将所有这些错误重定向到它们的正确"URL(例如 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,将所有 500 重定向到另一个 URL(替换为您的域).

The ErrorDocument redirects all 404s to a specific URL, all 500s to another url (replace with your domain).

重写规则将该 URL 映射到您的实际 404.php 脚本.如果您愿意,可以使 RewriteCond 正则表达式更通用,但我认为您必须明确定义要覆盖的所有 ErrorDocument 代码.

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 行(请参阅:Apache 错误文档Apache 自定义错误一>).上面的 .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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆