【问题标题】:wordpress .htaccess rule for folder structure giving 404 errorwordpress .htaccess 文件夹结构的规则给出 404 错误
【发布时间】:2017-11-30 11:59:57
【问题描述】:

我正在使用 wordpress 创建一个求职网站。我想创建一个自定义 .htaccess 规则,这样当用户在表单中搜索作业时,第一部分将作为文件夹结构,其余部分将作为查询参数添加。

只有当我们查询参数 ?find= any-value be in url 时才会这样 休息页面想要的样子 http://example.com/contact/

我想要:-

http://example.com/?find=any-value&job_location=Austin,TX&findjobs=Search

变成:-

http://example.com/any-value?job_location=Austin,TX&findjobs=Search

我的永久链接设置如下:-

http://example.com/sample-post/

QA .htaccess 代码

# Special redirects RewriteCond %{QUERY_STRING} ^find=([^/]*)$ RewriteRule ^/?$ /%1\/? [R=301,L] # BEGIN WordPress RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] # END WordPress

【问题讨论】:

    标签: wordpress .htaccess url-rewriting


    【解决方案1】:

    你应该在template_redirect钩子中使用wp_redirect函数。

    可以添加到functions.php

    例子:

    function my_redirect_function() {
        // check if argument is set
        if ( $_GET['find'] ) {
            // get site url
            $url = get_site_url() . '/' . $_GET['find'];
    
            // clone GET arguments
            $query = $_GET;
    
            // remove the find argument
            unset( $query['find'] );
    
            // add all get arguments to the new url
            $url = add_query_arg( $query, $url );
    
            // redirect to the generated url
            wp_redirect( $url );
            exit();
        }
    }
    
    add_action( 'template_redirect', 'my_redirect_function' );
    

    链接:

    【讨论】:

    • 如您所见,我的参数 ?find=any-value 在所有新搜索中采用不同的值。你能举个例子吗?
    • 未测试,但请说明您需要做什么
    • 我试过了,但没有运气,它正在重定向,但页面出现同样的 404 错误,并且在重定向后 $_GET['find'] 正在丢失。
    • 可能是因为该页面不存在。 (http://example.com/any-value?job_l...)。你用这个 slug 创建了一个页面吗?
    【解决方案2】:

    +1 给你 Maxwell 非常感谢你下面是其他人的工作代码;)

    add_action('parse_request', 'vm_parse_request');
    
    function vm_parse_request(&$wp) {
        if (isset($_GET['find'])) {
            // get site url
            $url = get_site_url() . '/' . $_GET['find'];
            // clone GET arguments
            $query = $_GET;
            // remove the find argument
            unset($query['find']);
            // add all get arguments to the new url
            $url = add_query_arg($query, $url);
            // redirect to the generated url
            wp_redirect($url);
            exit();
        }
        if (isset($_GET['findjobs'])) {
            // setup hooks and filters to generate virtual movie page
            add_action('template_redirect', 'vm_template_redir');
            //add_filter('the_posts', 'vm_createdummypost');
            //add_filter('the_content', 'j2c_job_display');
            // remove_filter('the_content', 'wpautop');
        }
    }
    
    function vm_template_redir() {
        // Reset currrently set 404 flag as this is a plugin-generated page
        global $wp_query, $post;
        $wp_query->is_404 = false;
        header("HTTP/1.1 200 OK");
        $urlslug = explode("/", $_SERVER['REQUEST_URI']);
        $urlslug = explode("?", $urlslug[1]);
        $post = get_post(165, OBJECT);
        //$post->content = "hello testhere";
        setup_postdata($post);
        add_action('wp_enqueue_scripts', 'j2c_enqueue', 9999);
        $_GET['find'] = str_replace("/", "", $urlslug[0]);
       // add_filter('the_content', 'j2c_job_display');
        wp_reset_postdata();
        $template_path = plugin_dir_path(__FILE__) . 'template/page.php';
        include( $template_path); // child
    
        exit;
    }
    

    【讨论】:

      猜你喜欢
      • 2014-08-08
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      • 2018-10-11
      • 1970-01-01
      • 1970-01-01
      • 2022-09-22
      相关资源
      最近更新 更多