syslogic syslogic - 8 months ago 54
PHP Question

.htaccess to WP_Rewrite regex pattern

I have a re-write rule in .htaccess (it's working fine) that I'd like to add as WordPress wp_rewrite -
mainly for portability and user's convenience... The question is just how the regex-pattern has to look like...

RewriteCond %{REQUEST_URI} ^/services/rest/(.*)$
RewriteRule ^(.*)/(.*)/(.*)$ wp-content/plugins/some_plugin/api.php?action=$3 [L,NC]

The syntax that I would require looks about like this:

$newrules = array('^/services/rest/(.*)' => mAPI_PLUGIN_URL.'/socket.php?action=$matches[1]');

Any suggestions how to solve or how to improve that?


Add the following code in your script/theme:

add_action('init', 'add_rewrite_rule');
add_filter('query_vars', 'query_vars');
add_filter('wp_head', 'rooter');

function add_rewrite_rule()
    add_rewrite_rule('services/rest/(.*)', 'index.php?my_action=$matches[1]', 'top');

function query_vars($query_vars)
    $query_vars[] = 'my_action';
    return $query_vars;

function rooter()
        // Do your tasks here

In my case it is working good for a plugin I have developed for personal use.