最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

url rewriting - Rewrite rule shows 404 page

programmeradmin0浏览0评论

I am new to Wordpress so I don't really know how the rewrite rules should look like. My problem is that I have a custom template in my theme called portfolio-inner.php

My rule looks something like that ['portfolio/php/wordpress'] => 'portfolio-inner/?galid=3'.

The problem is that all the examples show an index.php URL as the destination, but I don't know how to build the index.php URL for my template URL.

As a result, that rule shows the 404 page.

As a workaround, I edited the .htaccess and added this:

Redirect /portfolio/php/wordpress  /?galid=3

and I see the right gallery page, but that's not OK, because the browser's address bar changes and it should remain /portfolio/php/wordpress

I am new to Wordpress so I don't really know how the rewrite rules should look like. My problem is that I have a custom template in my theme called portfolio-inner.php

My rule looks something like that ['portfolio/php/wordpress'] => 'portfolio-inner/?galid=3'.

The problem is that all the examples show an index.php URL as the destination, but I don't know how to build the index.php URL for my template URL.

As a result, that rule shows the 404 page.

As a workaround, I edited the .htaccess and added this:

Redirect /portfolio/php/wordpress  http://example.com/portfolio-inner/?galid=3

and I see the right gallery page, but that's not OK, because the browser's address bar changes and it should remain /portfolio/php/wordpress

Share Improve this question edited Dec 5, 2013 at 13:46 s_ha_dum 65.5k13 gold badges84 silver badges174 bronze badges asked Dec 5, 2013 at 11:46 bogdanbogdan 1011 silver badge1 bronze badge 1
  • You can't rewrite requests directly to template files, that's not how WordPress works. Rewrite rules typically point to a WordPress object of some sort - a page, an archive, a post, etc., via query arguments appended to the main index.php file, which then loads a template file based on the type of request, following the template hierarchy. – Milo Commented Dec 5, 2013 at 16:14
Add a comment  | 

2 Answers 2

Reset to default 1

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' );

I ended up adding a rewrite rule to a page the uses my template. Also, I had to add galid as a rewrite endpoint for EP_PAGES. This tutorial helped a lot: http://wp.tutsplus.com/tutorials/creative-coding/the-rewrite-api-the-basics/

发布评论

评论列表(0)

  1. 暂无评论