0

I have a folder with name:

wordpress_install/dev/abc

Now, I have created a index.php in abc folder and I want all the urls like:

wordpress_install/dev/abc/asdfasdfsdf (a 404 page)

to be redirected to

wordpress_install/dev/abc/index.php

My motive that any request (404 or anyt other) should go to index.php in subfolder. But, the original wordpress should run as it is.

My current .htaccess in subfolder is:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*)$ /index.php?path=$1 [NC,L,QSA]
ErrorDocument 404 /index.php

The Wordpress .htaccess is as it is.

5 Answers 5

0

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>";
0

Don't get it complex. Just open your active theme's 404.php And use wp_safe_redirect and make it open home page. Here is the example code.

   <?php // 404.php

          wp_safe_redirect(home_url()); 

     ?>

I guess it solves your problem.

0

In the order of execution dictated by the wordpress default htaccess, apache first checks if a file matching the URL exists on the server and if it doesn't it runs wordpress which parses the URL. Therefor it is safe to assume that any URL of the kind of dev/abc/* reprecent a file that wasn't found.

What you can do is to match the $_SERVER['REQUEST_URI'] against that pattern and if it matches redirect to /dev/abc/index.php.

There are actually several 301 redirect plugins that should be able to do pattern matching and redirect of this kind.

0

Another alternative is to simply exclude you directory out of the wordpress htaccess rules.

(my htaccess mojo is very weak before I drink 4 cups of coffee so this is as deep as I can get at this time of the day)

0

The site and subdirectory has been added into following code:

Blog url is set to http://www.domain.com/subdirectory

Home Url http://www.domain.com

htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /subdirectory/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /subdirectory/index.php [L]
</IfModule>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.