Gallery2 rewrites display Wordpress home

edam

Joined: 2008-08-02
Posts: 11
Posted: Sat, 2008-08-02 23:21

Hi there,

I just wanted to first say thanks for the WPG2 plugin - it's great!

I'm having a problem with those pesky rewrites though. :o( I have G2 setup and working perfectly at www.domain.com/xxx/new/gallery2. It's rewrite plugin is enabled and uses the rule "gallery/%path%" for showing items. Like I say, this works fine.

Wordpress is set up at www.domain.com/xxx/new. Accessing the gallery embedded via WPG2, I get the first page of the gallery at www.domain.com/xxx/new/wpg2, and the links to albums appear to be correct (e.g., www.domain.com/xxx/new/gallery/whatever) but they don't work. When you visit the URLs for the albums, there is no 404 error, but the Wordpress "home" page is displayed!!?

If I disable G2's redirects, it works ok for the time being, but I hate those URLs...

Any ideas?

--
www.waxworlds.org/edam

 
ozgreg
ozgreg's picture

Joined: 2003-10-18
Posts: 1378
Posted: Sun, 2008-08-03 00:25

edam,

Sounds like a classic Wordpress / Gallery2 rewrite fight.. What permalink rule (Wordpress) are you using??

____________________________________
Wordpress / Gallery2 (WPG2) Plugin, , WPG2 Documentation, WPG2 Demo

 
edam

Joined: 2008-08-02
Posts: 11
Posted: Sun, 2008-08-03 18:46

Hi ozgreg, thanks for the reply!

My Wordpress permalink settings are on "Month and name" (using rewrite rules of the form "/%year%/%monthnum%/%postname%") but I've tried custom rewrite rules, like "/posts/%postname%", and the problem still occurs.

Whats odd is that I have another WP & G2 setup with exactly the same settings that works fine on the same server with the same apache settings, etc! I've literally been through the settings screens of WP and G2 page by page and they match exactly! The only difference is that the installation that doesn't work is a fresh install of WP v2.6, G2 v2.2.5 and WPG2 v3.0.7 whereas I upgraded the other installation (unless I missed something!). I've even 'diff'ed the WP .htaccess and G2 .htaccess files and, except for the path differences, they match exactly.

Any insight you can provide will be warmly welcomed! Is there any more info I can give you to help diagnose the problem?

Thanks again.

--
www.waxworlds.org/edam

 
ozgreg
ozgreg's picture

Joined: 2003-10-18
Posts: 1378
Posted: Sun, 2008-08-03 23:06

edam,

Not a lot of insights in debugging this kind of behavior, I seen issues like this before when you cannot simply navigate to the albums even although the links work. The central issue is I cannot get a rewrite debug log from Wordpress because it internally handles rewrites rather than utilizing the apache rewrite module (which can be logged). I am certainly not given up getting some kind of log for Wordpress just a little stumped how this can be done without hacking Wordpress..

I be happy to look at the debug logs if you want to install BTEV debugger and send me the debug log when you click on the WPG2 Gallery2 Rewrite Tab (That should give me the rules being inserted into the Wordpress Permalink Array)

____________________________________
Wordpress / Gallery2 (WPG2) Plugin, , WPG2 Documentation, WPG2 Demo

 
edam

Joined: 2008-08-02
Posts: 11
Posted: Mon, 2008-08-04 16:05

No problems...

Right, requesting the WPG2 Gallery2 Rewrite page causes the following BTEV logs (excuse the editing):

NOTICE  /home/xxx/home/new/wp-content/plugins/wpg2/wpg2embed.inc  	1  	WPG2 ISDONE()
NOTICE 	/home/xxx/home/new/wp-content/plugins/wpg2/wpg2managerewrites.inc 	1 	WPG2 G2 REWRITE PLUGIN VALIDATION PASSED
NOTICE 	/home/xxx/home/new/wp-content/plugins/wpg2/wpg2embed.inc 	1 	WPG2 ACTIVATED G2 REWRITE PLUGIN
NOTICE 	/home/xxx/home/new/wp-content/plugins/wpg2/wpg2embed.inc 	1 	WPG2 VERIFIED HTACCESS
NOTICE 	/home/xxx/home/new/wp-content/plugins/wpg2/wpg2embed.inc 	1 	WPG2 SET G2 REWRITE LOCATION /xxx/new/
NOTICE 	/home/xxx/home/new/wp-content/plugins/wpg2/wpg2embed.inc 	1 	WPG2 SET G2 REWRITE HTACCESS /home/xxx/home/new/
NOTICE 	/home/xxx/home/new/wp-content/plugins/wpg2/wpg2embed.inc 	1 	WPG2 G2 SESSION RESUMED
NOTICE 	/home/xxx/home/new/wp-content/plugins/wpg2/wpg2embed.inc 	1 	WPG2 LOGGED IN WP:1 G2 ID:6
NOTICE 	/home/xxx/home/new/wp-content/plugins/wpg2/wpg2embed.inc 	1 	WPG2 INITALISED G2
NOTICE 	/home/xxx/home/new/wp-content/plugins/wpg2/wpg2functions.inc 	1 	WPG2 TEMPLATE PAGE SLUG VALIDATE

Interestingly though, when requesting an album via WPG2, BTEV logs the following:

WPG2 ISDONE()
WPG2 ADDED G2 TO WP HEADER
WPG2 G2 SESSION RESUMED
WPG2 LOGGED IN WP:1 G2 ID:6
WPG2 INITALISED G2

which looks the same as when accessing a non-WPG2 page (i.e., a normal Wordpress page). Should it have a WPG2 PAGE: 7 TRIGGER log message, like then you access the main gallery page via WPG2? Could it be an issue of WP not giving (or WPG2 not taking) control? I don't really know how the WP plugin mechanism works...

I'd be happy to install some hacked WP files if it will help track down the issue! :o)

Thanks ever so much for your help ozgreg.

--
www.waxworlds.org/edam

 
ozgreg
ozgreg's picture

Joined: 2003-10-18
Posts: 1378
Posted: Mon, 2008-08-04 22:40

What should happen and I say should is the Wordpress page hook gets fired sending control briefly to the WPG2 plugin template generation code which checks what Wordpress Page ID is about to be displayed and verifies if that page ID belongs to the WPG2 plugin, if not then it bounces control back.

The trick here is Wordpress should be directing the request via WPG2 template generation code, if it does not that is when the 404 errors come in.

You should only be seeing WPG2 INITALISED G2 events when a page has a G2 image, otherwise the WPG2 plugin will not call on G2 to be activated

Ok lets go back a few steps and just verify rewrites are the cause by turning off the G2 rewrite plugin via the WPG2 Gallery2 Rewrite Tab.

You should be able to navigate to all your albums..

____________________________________
Wordpress / Gallery2 (WPG2) Plugin, , WPG2 Documentation, WPG2 Demo

 
edam

Joined: 2008-08-02
Posts: 11
Posted: Tue, 2008-08-05 08:08
Quote:
The trick here is Wordpress should be directing the request via WPG2 template generation code, if it does not that is when the 404 errors come in.

Ok, well that appears to be working then because the rewritten urls to the album pages don't cause 404 errors.

Quote:
You should only be seeing WPG2 INITALISED G2 events when a page has a G2 image

Really? When I view the Wordpress front page I also get WPG2 INITALISED G2 log events. In fact, I get them when I view any WP page or post (not the admin pages) by the looks of it!

Quote:
You should be able to navigate to all your albums..

Yup, with rewrites turned off (via WPG2 rewrites tab), I can view the albums fine.

--
www.waxworlds.org/edam

 
ozgreg
ozgreg's picture

Joined: 2003-10-18
Posts: 1378
Posted: Tue, 2008-08-05 12:48
Quote:
You should only be seeing WPG2 INITALISED G2 events when a page has a G2 image

Really? When I view the Wordpress front page I also get WPG2 INITALISED G2 log events. In fact, I get them when I view any WP page or post (not the admin pages) by the looks of it!

Does the page have G2 content because I am not seeing this behaviour in my test blog?

Quote:
You should be able to navigate to all your albums..

Yup, with rewrites turned off (via WPG2 rewrites tab), I can view the albums fine.

And once you turn on Rewrites you get 404 errors? or you cannot click below an album (that is see the content)

____________________________________
Wordpress / Gallery2 (WPG2) Plugin, , WPG2 Documentation, WPG2 Demo

 
edam

Joined: 2008-08-02
Posts: 11
Posted: Tue, 2008-08-05 18:33
Quote:
Does the page have G2 content because I am not seeing this behaviour in my test blog?

No, the page definitely doesn't have G2 content. It is a static Wordpress "page" (not a post). And I'm definitely getting the WPG2 INITALISED G2 event logged.

Quote:
And once you turn on Rewrites you get 404 errors? or you cannot click below an album (that is see the content)

I don't get 404 errors, the blog front page is displayed when I try to view any album. Only G2's front page and admin pages actually work via WPG2. Viewing them normally, not via WPG2, everything works fine.

I'll look in to both these issues more over the weekend and report back on what I find... I haven't really got time during the week. :o( If you've got any ideas on where I should start looking, or tips on what to try, any advise would be appreciated!

--
www.waxworlds.org/edam

 
edam

Joined: 2008-08-02
Posts: 11
Posted: Wed, 2008-08-06 17:31

Hi ozgreg,

I think I've found the problem...

WPG2 is adding empty rewrite rules to WP. I.e., the rewrite rules go to empty strings. The cause of this is line 431 of wpg2functions.inc which looks like this:

$newrules[$wpg2rewriterule] = preg_replace($wpDirectory,'',$rule['substitution']);

This is not correct: $wpDirectory is a URI and is not suitable to be used in a regular expression since it could have regex special characters in. In my case, $wpDirectory has the value '/xxx/new/' which is an invalid regex (since it has 3 '/'s in it) and was causing preg_replace() to return an empty string.

I also have a suggested fix. Replace the offending line with the following code:

if( substr( $rule['substitution'], 0, strlen( $wpDirectory ) ) == $wpDirectory )
	$newrules[$wpg2rewriterule] = substr( $rule['substitution'], strlen( $wpDirectory ) );
else
	$newrules[$wpg2rewriterule] = $rule['substitution'];

This works, I have tested it. Sorry it's not a nice one-liner like the line it replaced! :o(

It's not even safe to use str_replace() instead though. Image if $wpDirectory held the value /foo/ and was being tested against the string /foo/bar/foo/index.php?page_id=7&g2_path=%1 - it would replace all instances of "/foo/".

This still hasn't solved the problem of WPG2 initialising G2 on every WP page request. I'll have a look in to that tomorrow or at the weekend and report back. Edit: I've looked in to the "WPG2 initialising G2 on every WP page request" issue a bit as well now. As far as I can tell, this is behavior by design. WPG2 uses WP's wp_head hook, which is fired on entering the <head></head> section of the page. This in turn calls g2_addheader(). The first thing g2_addheader() does is check to see if G2 is initialised and call g2_login() if it isn't. It does seem strange though that G2 should be initialised on every WP page request...

Anwyay, I hope I've helped. I always feel good about contributing to FLOSS, even if only very slightly. ;o) Once again, thanks ever so much for WPG2, it is a fantastic plugin!

--
www.waxworlds.org/edam

 
ozgreg
ozgreg's picture

Joined: 2003-10-18
Posts: 1378
Posted: Thu, 2008-08-07 01:59

Thanks for this I will open a bug report against this and check it out for you..

____________________________________
Wordpress / Gallery2 (WPG2) Plugin, , WPG2 Documentation, WPG2 Demo

 
genbian

Joined: 2009-02-05
Posts: 14
Posted: Tue, 2009-02-24 04:12

Thanks for this hard to hunt down bugfix!
Getting a WP + G2 + WPG2 Setup that is working (with rewrites) took me a whole weekend!
(And now I'm using old Versions: Gallery 2.2.5, WP 2.5.1 and WPG2 3.05 with this patch. I'm glad that it is working and I'm not going to change it! [in the near future])

genbian