繁体   English   中英

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

[英].htacess route url1 to url2 without changing the browser url

现在在用wordpress实现结果时遇到问题,想使用htacess做到这一点,想制作url

http://example.com/blog-preview/$any通过http://example.com/blog/$any访问

而不更改浏览器网址,即http://example.com/blog/$any

我不会使用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' );
?>

暂无
暂无

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

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