.htacess将url1路由到url2而不更改浏览器url

时间:2018-10-31 08:08:59

标签: wordpress .htaccess

现在用wordpress实现结果时遇到问题,想使用htacess来完成此操作,

http://example.com/blog-preview/$any可被http://example.com/blog/$any

访问

无需更改浏览器网址即http://example.com/blog/$any

1 个答案:

答案 0 :(得分:0)

我不会使用htaccess文件执行此操作。原因是htaccess文件只能重定向内容,但是-如果无法将内容从一页移动到另一页,这听起来就是您所追求的。

我可以通过以下方法来实现此目的:创建一个函数并将其放在functions.php中,然后使用file_get_contents从需要显示的页面中获取内容。

解决方案1:对所有页面进行标准化:

function custom20190126_preview_function() {

  $request_uri = $_SERVER['REQUEST_URI'];
  $target_uri = '/blog/';
  if( substr( $request_uri, 0, strlen( $target_uri ) === $target_uri ){
    echo file_get_contents( 'https://example.com/blog-preview/foobar' );
    die();
  }
}
add_action( 'template_redirect', 'custom20190126_preview_function' );

解决方案2:从特定的页面模板获取内容

如果要从WordPress的页面模板中执行此操作,则可以创建一个函数并从页面模板中调用它(例如single.phparchive.php)。像这样:

在archive.php中

<?php
echo file_get_contents( 'https://example.com/blog-preview/foobar' );
?>
相关问题