使用 htaccess 重定向到页面但保留原始 URL

Redirect to page but keep original URL using htaccess

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^test\.localhost/$ [NC]
RewriteRule ^(.*)$ http://test\.localhost/err.php/ [NC,L]

这行得通。当我输入 test.localhost/qwieoqjdoiqwje 时,它会将我重定向到:

测试.localhost/err.php/qwieoqjdoiqwje

但我想重定向我来重定向我测试。localhost/qwieoqjdoiqwje 并且还加载 err.php 。这可以通过 htaccess 实现吗?谢谢1

您可以通过使用 .htaccess

将所有请求重定向到一个文件来实现这一点
<IfModule mod_rewrite.c>

  # Block access to hidden files
  RewriteRule "(^|/)\." - [F]

  # Redirect requests that are not files or directories to index.php.
  RewriteEngine on
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_URI} !=/favicon.ico
  RewriteRule ^ index.php [L]
</IfModule>

然后在你的index.php中使用路径来执行你想要的任何功能。

  if ($path == 'error' || $path == 'hello/world')
  {
     myerrorfunction();
  }

这当然是一个非常简单的解决方案。您可能希望创建一个将路径映射到函数的数组。这就是大多数 cms 系统完成此任务的方式。