Rewrite rule shows 404 page

The index.php you see is not the template file. It is pointed to index.php file you see in the root of your WordPress installation.

How does the pretty URL translate into a template file

  1. When a person goes to a URL on your site, the .htaccess redirects all requests, that don’t match an actual file, to index.php to kick off the whole WordPress process.
  2. WordPress finds a match for the URL path to the rewrite rules using regular expressions.
  3. When a match is found, WordPress converts the regular expression for the rule into a query string. If you disable pretty URLs, WordPress will use the same query strings in the URLs.
  4. The query string is then parsed into the query vars which are defined variables. It will also fill in any implied vars.
  5. Then the default WP_Query object that is used in the main loop is created based upon the query vars.
  6. Then WordPress determines which template to load from the theme base upon the query vars or an override using the template_redirect hook.

How do you override this

Without fully knowing what you are trying to do, I’m going to answer your question as is. There are probably optimizations for what you are doing.

We will add the custom rewrite rule. You will need to update your permalinks to apply the changes.

function my_custom_rewrite_rules( $rewrite ) {
    $rewrite->rules['portfolio/php/wordpress'] = 'index.php?my_portfolio=1&galid=3';
}
add_action( 'generate_rewrite_rules', 'my_custom_rewrite_rules' );

Because we are using a custom query var my_portfolio, we need to register it with WordPress.

function my_custom_query_vars( $vars ) {
$vars[] = 'my_portfolio';
return $vars;
}
add_filter( 'query_vars', 'my_custom_query_vars' );

Now we can tell WordPress to use our custom template when our query var my_portfolio is true. To use our template, we use locate_template to see if the template exists and then call it. If the template exists, it returns true to tell it was found.

function my_custom_template_redirect() {
    global $wp_query;

    if ( $wp_query->get( 'my_portfolio' ) && locate_template( 'portfolio-inner.php', true ) ) {
        exit;
    }
}
add_action( 'template_redirect', 'my_custom_template_redirect' );