简体   繁体   中英

Wordpress - rewrite fake urls

I created a fake page in wordpress, that i catch with this code:

if ($_GET['fake_page'] == "myfakepage") {
  add_filter('the_title','plugin_myown_title');
  add_filter('the_content','plugin_myown_content');
  add_action('template_redirect', 'plugin_myown_template');
}

this works correctly. Now what I'd like to do is to rewrite such this url

my_site?fake_page=myfakepage

in

my_site/product/myfakepage

where product is an existing page created from wordpress dashboard.

I followed this guide:

http://statichtml.com/2010/mod-rewrite-baseon-on-query-string.html

and defined this rule:

RewriteEngine On
RewriteCond %{QUERY_STRING}     ^fake_page=(.*)$    [NC]
RewriteRule ^/$       my_site/product/%1      [NC,L,R=301]

but what i get is

my_site/product?fake_page=myfakepage

where is the problem? Besides I would like to know if typing into browser:

my_site/product/myfakepage

i could make wordpress treats it as

 my_site?fake_page=myfakepage

without any redirect. Thanks!

Try this code from your functions.php:

add_action( 'generate_rewrite_rules', 'my_rewrite_rules' );
function my_rewrite_rules( $wp_rewrite )
{
    $wp_rewrite->rules = array(
        'product/myfakepage/?$' => $wp_rewrite->index . '?fake_page=myfakepage',
    ) + $wp_rewrite->rules;
}

Note: you do not need that custom .htaccess. BTW: you souldn't modify .htaccess directly, never. There is an extensive Rewrite API in WordPress which allows you to apply custom rules.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM