重定向到页面,但使用 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/$1 [NC,L]

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

test.localhost/err.php/qwieoqjdoiqwje

但是我想重定向我以将我重定向到 test.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>

然后在索引中.php使用该路径执行所需的任何函数。

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

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