Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将子文件夹中的任何url/页面重定向到另一个url?_Php_Wordpress_.htaccess_Redirect - Fatal编程技术网

Php 如何将子文件夹中的任何url/页面重定向到另一个url?

Php 如何将子文件夹中的任何url/页面重定向到另一个url?,php,wordpress,.htaccess,redirect,Php,Wordpress,.htaccess,Redirect,我在Wordpress中有一个自定义帖子类型,如下slug: domain.com/folder/page-1 我想将所有/文件夹/链接/URL重定向到另一个URL 例如: domain.com/folder/page-1 domain.com/folder/page-2 domain.com/folder/page-3 全部重定向到domain.com/some页面怎么样: RewriteEngine on RewriteRule ^domain.com/folder/(.*)$ domain

我在Wordpress中有一个自定义帖子类型,如下slug:

domain.com/folder/page-1

我想将所有/文件夹/链接/URL重定向到另一个URL

例如:

  • domain.com/folder/page-1
  • domain.com/folder/page-2
  • domain.com/folder/page-3
  • 全部重定向到domain.com/some页面

    怎么样:

    RewriteEngine on
    RewriteRule ^domain.com/folder/(.*)$ domain.com/some-page [R=301,L]
    

    您可以为此使用重定向匹配指令:

    RedirectMatch ^/folder/.+$ http://example.com/page
    

    如果您有许多URL要重定向,最好使用插件,建议使用插件

    另一种方法是使用wordpress函数wp_redirect()

    add_action('template_redirect'、'redirect_page\u other_page');
    如果(!function_存在('redirect_page_另一个_page')){
    函数重定向\u页面\u另一个\u页面(){
    $queryed_post_type=get_query_var('post_type');
    if(is_single()&&“custom_post_type_name”==$queryed_post_type){
    $redirect_url=site_url()。/folder/some page/;
    wp_重定向($redirect_url);
    出口
    }
    }
    }
    
    如果您使用的是WordPress,您可以在设置中更改永久URL。转到Dashboard>Settings>Permalinks>Post Name我不想更改整个站点的permalink结构。您可以使用
    RewriteCond
    ,但我不建议您这样做,因为.htaccess可能很难调试。你不能将所有内容合并到一个文件中吗?@Samuel因为我在WordPress中使用自定义的帖子类型,它会自动在“文件夹”子文件夹中添加“第1、2、3页…”。下面是我如何使用它。这是一个健身俱乐部的网站。他们在日历中添加了课程。为了在日历中添加类,他们必须在自定义的post类型中创建类,并且只包含标题。Wordpress或插件会自动为每个类提供自己的页面,我只希望它显示在日历上,而不是自己的页面上。就像这样,…/classes/Zumba,classes/yoga/这些都是空白页。我宁愿把它们都转发给你。等等,我还以为你是西班牙人呢!
    add_action('template_redirect', 'redirect_page_another_page');
    
    if(!function_exists('redirect_page_another_page')){
    
       function redirect_page_another_page(){
    
       $queried_post_type = get_query_var('post_type');
       <!-- replace custom_post_type_name with you custom post type name -->
      if ( is_single() && 'custom_post_type_name' ==  $queried_post_type ) {
      $redirect_url = site_url().'/folder/some-page/';
      wp_redirect( $redirect_url );
      exit;
      }
     }
    }