views:

209

answers:

3

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
+1  A: 

Try this rule:

RewriteRule ^fake/([^/]+)$ gallery?somevar=$1 [L]
Gumbo
Nope, that's the first thing I tried and it just ends in a 404 from wordpress.
kemp
A: 

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.

Conspicuous Compiler
I tried PT too, but it doesn't help. I believe the only way to do it is via wordpress' internal rewrite as Richard M said in a comment.
kemp
A: 

Solved, it was the Permalink Redirect plugin which needed to be configured to skip the desired "virtual" URLs, else it would issue a 301 Permanently moved.

kemp