简体   繁体   English

重定向到页面,但使用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]

This works. 这可行。 When I type test.localhost/qwieoqjdoiqwje it redirects me to: 当我输入test.localhost / qwieoqjdoiqwje时,它会将我重定向到:

test.localhost/err.php/qwieoqjdoiqwje test.localhost / err.php / qwieoqjdoiqwje

But I want to redirect me to redirect me to test.localhost/qwieoqjdoiqwje and also load err.php . 但是我想重定向我以将我重定向到test.localhost / qwieoqjdoiqwje并还加载err.php。 Is this possible via htaccess? 可以通过htaccess吗? Thanks1 Thanks1

You can accomplish this by redirecting all requests to one file with .htaccess 您可以通过使用.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>

Then in your index.php use the path to execute whatever function you want. 然后在index.php中,使用路径执行所需的任何功能。

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

This is of course a very simple solution. 这当然是一个非常简单的解决方案。 You would probably want to create an array that maps a path to a funciton. 您可能想要创建一个将路径映射到功能的数组。 this is how most cms systems acomplish this task. 这就是大多数cms系统完成此任务的方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM