重定向 404 错误 htaccess 后重写 URL

2023-12-09

所以我知道这可能看起来有点奇怪,但为了保持一致性,我希望所有网址都以这种形式出现:

http://example.com/page/ 

到目前为止,我已经使常规页面正常工作,但我似乎无法使错误页面正常工作。

如果用户访问不存在的页面或目录,我希望浏览器硬重定向到:

http://example.com/404/ 

然而,该目录实际上并不存在。错误页面的真实位置将在下面

/pages/errors/404.php

另外,虽然我不需要所有各种错误(400、401、403、404、500)的确切答案,但我将应用给出的任何方法将所有这些错误重定向到它们的“正确”URL 例如。

http://example.com/400/ 
http://example.com/500/ 

etc.

有任何想法吗?


在你的 .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]

The ErrorDocument将所有 404 重定向到特定 URL,将所有 500 重定向到另一个 url(替换为您的域)。

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

本地重定向:

将 .htaccess ErrorDocument 更改为存在的文件(必须存在,否则会收到错误):

ErrorDocument 404 /pages/errors/404_redirect.php

404_redirect.php

<?php
   header('Location: /404/');
   exit;
?>

根据错误号重定向

看起来您需要指定一个ErrorDocument.htaccess 中针对您要重定向的每个错误的行(请参阅:Apache 错误文档 and Apache 自定义错误). The .htaccess上面的例子中有多个例子。您可以使用以下内容作为通用重定向脚本来替换上面的 404_redirect.php。

错误重定向.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;
?>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

重定向 404 错误 htaccess 后重写 URL 的相关文章

  • $1 [QSA,L] 在我的 .htaccess 文件中意味着什么?

    我需要改变我的 htaccess有两行我不明白 RewriteCond REQUEST FILENAME l RewriteRule index php url 1 QSA L 我什么时候应该使用这些线 不是提供完整教程的地方 但这里是简短
  • 将 mod-rewrite 添加到现有 PHP 网站

    我正在更新一个 php 应用程序 该应用程序当前不使用 url 重写 目的是隐藏文件扩展名 网站总体结构如下 root index php login php page1 php page2 php page3 php page4 php
  • https 重定向 laravel .htaccess 之后删除 /public

    我有一个 Laravel 页面部署在共享主机中 当我强制 http 请求重定向到 https 时 url 包含 public 我的根 htaccess 是 RewriteEngine on RewriteCond REQUEST URI p
  • 使用 .htaccess 进行 PHP 设置时出现 500 内部服务器错误

    当我使用时 htaccess对于以下 PHP 设置 我得到500 Internal Server Error访问网站时 中的代码 htaccess file php flag display errors off php flag log
  • 使用 htaccess 阻止特定文件扩展名?

    我使用此代码将所有请求发送到单个 php 文件 RewriteEngine On RewriteCond REQUEST FILENAME f RewriteCond REQUEST FILENAME d RewriteRule myfil
  • Facebook 身份验证无需重定向?

    有没有办法在不重定向的情况下使用 facebook 身份验证 OAuth 2 0 我没有使用 facebook 登录按钮 所以我应该重定向到https www facebook com dialog oauth https www face
  • 如何在不更改地址栏中的 URL 的情况下重定向到不同的域?

    我想重定向自 域名1 com 照片 To 域名2 com 照片 我希望地址栏中的 URL 仍显示为 域名1 com 照片 有没有办法只使用 htaccess 来做到这一点 Note 我的domain1 com htaccess 文件目前完全
  • .htaccess 主要用于 localhost

    我在本地主机上使用 wamp server 虚拟主机名为www xyz com我有一个通过 git 设置的部署服务 它将代码部署到服务器上www xyz in 这是我的 htaccess 代码 RewriteEngine on Rewrit
  • 改进将字符串转换为可读的 url

    以下函数重写包含各种字符的新闻和产品标题的 url 我希望创建的字符串仅包含字母数字值和 但没有结尾 或空格 也没有重复的 下面的函数工作正常 但我想知道是否有任何方法可以将其编写得更简单或更有效 function urlName stri
  • 从 Laravel 的 URL 中删除 index.php

    我正在 Linux Mint 中使用 Laravel 5 5 12 我正在使用 LAMP 堆栈 我想删除index php来自网址 我的mod rewrite已启用阿帕奇模块 My htaccess文件位于public文件夹 它包含以下代码
  • Javascript 函数与 php 一样吗?

    我在网站上使用 WebIM 提供聊天支持 我希望能够在客户端启动聊天会话时设置一个计时器 如果操作员 技术人员在 x 秒内没有响应 我希望页面重定向到客户端可以留言的另一个页面 有点像 请稍等 我们尝试联系您 这样 如果所有技术人员都太忙或
  • Zend Framework 2 重写 URL

    我有一个 Zend Framework 2 应用程序 我希望使用 htaccess 重写一些 URL htaccess 文件位于我的公用文件夹中 我已经验证了一个简单的重写规则 它将所有 testurl 重写为 test php Rewri
  • 为什么在.htaccess中使用[QSA]时$_POST为空?

    我在 htaccess 中有以下内容 RewriteCond REQUEST URI app index php RewriteRule htm index php url 0 QSA L 一切都很好 但结果 URL 的 POST 为空 我
  • 配置 htaccess 以使用 Angular 和 PHP 路由

    我正在尝试使用 Angular 4 和 PHP 路由 但我无法配置它以便同时使用两者 我可以让它与其中之一一起工作 但不能同时与两者一起工作 这是我的文件夹结构 root index html vendor bundle js other
  • 使用 htaccess 文件重定向[关闭]

    Closed 这个问题是无关 help closed questions 目前不接受答案 Edit 我想重定向一个网址 to www example com location sydney from www example com rss
  • 如何在嵌入式 Jetty 中配置 ErrorPageErrorHandler?

    是否可以将 ErrorPageErrorHandler 配置为在未找到内容 服务时重定向到静态页面 这是我的代码 server new Server port Resource webRoot Resource newResource we
  • nginx proxy_pass 省略路径

    我已经配置了 nginx 反向代理 location root var www html index index html location login proxy pass http 127 0 0 1 9080 proxy set he
  • Php mod_rewrite 无法正常工作

    我有一个带有以下链接结构的 php 页面 http localhost wisper businesspage php profile creativeartbd 所以我尝试将此链接转换为以下样式 http localhost wisper
  • .htaccess 重定向到随机 URL

    我的服务器上有几个页面有流量 我需要将它们重定向到其他网站 这是最简单的部分 问题是我需要将事情混合起来并随机将人们发送到新站点 我发现可以在 time sec 之前做到这一点 但无论我尝试什么 我目前的知识都不足以使其发挥作用 这是我现在
  • CodeIgniter 中的 base_url() 和 403 错误

    在 HTML 视图中 css 链接代码如下所示 但我的浏览器显示的地址具有双域地址 www jedendzien pl www jedendzien pl assets css style css 所以我收到 403 错误 我通过以下方式创

随机推荐