How to redirect all 404 in a WordPress subdirectory to the index.php of subdirectory?

You can use a custom endpoint. Just be sure you flush the rewrite rules after it has been added/changed – not every page load!

This will turn http://yoursite.com/dev/abc/asdfasdfsdf into http://yoursite.com/dev/abc/index.php?whatever_you_want_your_index_to_see=asdfasdfsdf

More Info on rewrite rules: http://code.tutsplus.com/articles/the-rewrite-api-the-basics–wp-25474.

After you add a rewrite rule like add_rewrite_rule('^dev/abc/([^/]+)/?$', 'index.php?'.self::ENDPOINT_SYMBOL.'=1&' . self::ENDPOINT_PARAM . '=$matches[1]', 'top'); You can use this great plug-in to check how well your regex is working: https://wordpress.org/plugins/monkeyman-rewrite-analyzer/

<?php

////////////////////////////////////////////////////////////////////////////////////////

/**
 *      ONLY DO THIS ONCE!!!!! LIKE AFTER A THEME SWITCH
 *      FOR THIS DEMO --- MAKE SURE IT'S WORKING FIRST -- THEN COMMENT THIS OUT!!!
 */
function for_demo_only_after_endpoint_added()
{
    flush_rewrite_rules(false);
}
add_action('init', 'for_demo_only_after_endpoint_added', 99 );


////////////////////////////////////////////////////////////////////////////////////////

if (!class_exists('ExampleEndpoint')) :

    class ExampleEndpoint
    {

        const ENDPOINT_SYMBOL = '__dev/abc__';
        const ENDPOINT_PARAM = 'special_var';

        public function __construct()
        {
            add_filter('query_vars', array($this, 'add_query_vars'), 0);
            add_action('parse_request', array($this, 'sniff_requests'), 0);
            add_action('init', array($this, 'add_endpoint'), 0);
        }

        public function add_query_vars($vars)
        {
            $vars[] = self::ENDPOINT_SYMBOL; // use to snif the request
            $vars[] = self::ENDPOINT_PARAM;
            return $vars;
        }

        public function add_endpoint()
        {
            // if you change this, you'll need to flush the rewrite rules!
            add_rewrite_rule('^dev/abc/([^/]+)/?$', 'index.php?' . self::ENDPOINT_SYMBOL . '=1&' . self::ENDPOINT_PARAM . '=$matches[1]', 'top');
        }

        public function sniff_requests()
        {
            global $wp;
            if (isset($wp->query_vars[self::ENDPOINT_SYMBOL])) {
                $this->handle_request();
                exit;
            }
        }

        protected function handle_request()
        {
            global $wp;
            $request_args = array();

            // gather your variables
            if (isset($wp->query_vars[self::ENDPOINT_PARAM])) {
                $request_args[self::ENDPOINT_PARAM] = $wp->query_vars[self::ENDPOINT_PARAM];
            }

            // redirect to your index.php page - add the extra info as query args
            $location = home_url($path="/dev/abc/index.php?" .
                'whatever_you_want_your_index_to_see=" . $request_args[self::ENDPOINT_PARAM], $scheme = "http');


            // go now!
            $status = 301;
            wp_redirect($location, $status);
            exit; // fin
        }

    }

    $ep = new ExampleEndpoint();

endif;

In your index.php you can check to see if the query vars came through;

<?php

echo "You made it!<pre>";

var_dump($_REQUEST);

echo "<pre>";

Leave a Comment