Wordpress pages address rewrite

Posted by kemp on Stack Overflow See other posts from Stack Overflow or by kemp
Published on 2010-03-13T14:39:55Z Indexed on 2010/04/15 10:43 UTC
Read the original article Hit count: 250

UPDATE

I tried using the internal wordpress rewrite. What I have to do is an address like this:

http://example.com/galleria/artist-name

sent to the gallery.php page with a variable containing the artist-name.

I used these rules as per Wordpress' documentation:

// REWRITE RULES (per gallery) {{{
add_filter('rewrite_rules_array','wp_insertMyRewriteRules');
add_filter('query_vars','wp_insertMyRewriteQueryVars');
add_filter('init','flushRules');

// Remember to flush_rules() when adding rules
function flushRules(){
    global $wp_rewrite;
    $wp_rewrite->flush_rules();
}

// Adding a new rule
function wp_insertMyRewriteRules($rules)
{
    $newrules = array();
    $newrules['(galleria)/(.*)$'] = 'index.php?pagename=gallery&galleryname=$matches[2]';
    return $newrules + $rules;
}

// Adding the id var so that WP recognizes it
function wp_insertMyRewriteQueryVars($vars)
{
    array_push($vars, 'galleryname');
    return $vars;
}

what's weird now is that on my local wordpress test install, that works fine: the gallery page is called and the galleryname variable is passed. On the real site, on the other hand, the initial URL is accepted (as in it doesn't go into a 404) BUT it changes to http://example.com/gallery (I mean it actually changes in the browser's address bar) and the variable is not defined in gallery.php.

Any idea what could possibly cause this different behavior?

Alternatively, any other way I couldn't think of which could achieve the same effect described in the first three lines is perfectly fine.


Old question

What I need to do is rewriting this address:

(1) http://localhost/wordpress/fake/text-value

to

(2) http://localhost/wordpress/gallery?somevar=text-value

Notes:

  • the remapping must be transparent: the user always has to see address (1)
  • gallery is a permalink to a wordpress page, not a real address

I basically need to rewrite the address first (to modify it) and then feed it back to mod rewrite again (to let wordpress parse it its own way).

Problems

if I simply do

RewriteRule ^fake$ http://localhost/wordpress/gallery [L]

it works but the address in the browser changes, which is no good, if I do

RewriteRule ^fake$ /wordpress/gallery [L]

I get a 404. I tried different flags instead of [L] but to no avail. How can I get this to work?

EDIT: full .htaccess

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On

RewriteRule ^fake$ /wordpress/gallery [R]

RewriteBase /wordpress/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
</IfModule>

# END WordPress

© Stack Overflow or respective owner

Related posts about mod-rewrite

Related posts about Wordpress