Converting WordPress URL path into query string

I have a WordPress page that accepts a query string argument:

http://x.com/page-name/?parameter=value

Read More

This works fine. The page gets the value of $_GET[‘parameter’] correctly.

What I want to do is make it possible to type this as a normal URL:

http://x.com/page-name/value

I need the ability to rewrite the URL so the user enters URL 2, and WordPress gets URL 1. I am using Apache and would prefer to do this with mod_rewrite and .htaccess. Any advice?

Related posts

Leave a Reply

1 comment

  1. You can do this within WP using your theme’s functions.php:

    add_action( 'init', 'ss_permalinks' );
    function ss_permalinks() {
        add_rewrite_rule(
            'page/remove/([^/]+)/?',
            'index.php?pagename=page&service=$matches[1]',
            'top'
    );
    }
    add_filter( 'query_vars', 'ss_query_vars' );
    function ss_query_vars( $query_vars ) {
        $query_vars[] = 'removeid';
        return $query_vars;
    }
    

    Re-save your permalink settings once after implementing. page is the slug of the page to point to when the user access this URL (domain.com/page/remove/432), and $matches[1] should be the number after remove/ in the URL. This number is accessible by the variable specified later, $query_vars[] = 'removeid';/ $removeid on the target page’s template will be the number in the URL, if specified.