【问题标题】:How to redirect to 404 but keep wrong URL on address bar using .htaccess如何重定向到 404 但使用 .htaccess 在地址栏上保留错误的 URL
【发布时间】:2021-04-18 03:48:46
【问题描述】:

使用 .htaccess,我正在尝试重定向到 404,并在我的网站上遵循以下规则,该规则运行良好

RewriteEngine ON
ErrorDocument 404 thedomain.com/404
RewriteCond %{REQUEST_URI} !\.php/?$ [NC]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [QSA,L]

但正如预期的那样,这也会更改/重定向 URL。例如,如果没有名为 dummy.php 的文件名,则 URL 将重定向到 thedomain.com/404

我怎样才能保留现有的 URL (thedomain.com/dummy) 并重定向到 thedomain.com/404,就像 WordPress 404 页面中发生的那样。

【问题讨论】:

标签: apache .htaccess mod-rewrite errordocument


【解决方案1】:

让您的.htaccess 代码如下:

RewriteEngine On

ErrorDocument 404 /2021/404.php

RewriteCond %{DOCUMENT_ROOT}/2021/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

当您在ErrorDocument 404 中有绝对 URL 时,它将执行完全重定向。但是,使用相对 URL 只会进行内部重写,以防止 URL 在浏览器中更改。

确保在测试前清除浏览器缓存。

【讨论】:

  • 老样子!网址也在变化
  • 我刚刚从这里复制了所有内容
  • 清除缓存后我也收到Not Found .The requested URL was not found on this server. Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
  • 我什至在另一个浏览器中检查它并且是相同的。是的thedomain.comDocumentRoot 中的一个文件夹,但我在这个文件夹中有.htaccess
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-22
  • 1970-01-01
  • 2017-04-07
  • 1970-01-01
  • 2016-04-23
  • 2017-05-09
  • 2012-03-17
相关资源
最近更新 更多