WordPress pages address rewrite

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.

Read More

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

Related posts

Leave a Reply

4 comments

  1. Apologies for the very-delayed answer here, but I believe what you need to do is what Gumbo suggested but with the addition of the passthrough/PT flag:

    This flag forces the rewrite engine to set the uri field of the internal request_rec structure to the value of the filename field.

    You must use this flag if you want to mix directives from different modules which allow URL-to-filename translators.

    Although WordPress (via mod_php) runs after mod_rewrite is done, it normally gets the original (pre-rewrite) uri, and it uses that to do its own internal rewriting, rather than the re-written filename value.

    So try:

    RewriteRule ^fake/([^/]+)$ gallery?somevar=$1 [QSA,PT]
    

    I added QSA so that, just in case someone went to /wordpress/fake/something?page=3, it will get passed along properly. Let me know how that works.

    1. Using your favorite HTML/PHP editor, open wp-admin/includes/misc.php

    2. Find function function got_mod_rewrite Its around line 18 or so.

    3. Comment out the existing function: /* this is commented out */ — You can delete this function if you are sure the solution works, but to be safe, just comment it out in case you need to put it back.

    4. Add the following instead:

      function got_mod_rewrite() {
          $got_rewrite = true;
          return apply_filters(‘got_rewrite’, $got_rewrite);
      }
      
    5. Save and upload.