301 redirect URLs from g2_itemID=?? to /v/album/photo

daveozzz
daveozzz's picture

Joined: 2006-07-25
Posts: 70
Posted: Sun, 2008-02-10 17:18

Hi there,

I've just started running URL Rewrite to display my URLs from:
domain.com/gallery2/main.php?g2_itemId=num
to
domain.com/gallery2/v/album/filename.jpg.html

That's all great, but because my dynamic URLs are already indexed in Google I want to return a 301 Moved Permanently to any request for
domain.com/gallery2/main.php?g2_itemId=???
and redirect to
domain.com/gallery2/v/album/filename.jpg.html

Hopeful this would get Google to drop my dynamic URLs and not penalise me for duplicate content.
I am a total mod_rewrite novice. Can anyone point me in the right direction as how to do this? Or advise as to when I'm thinking along the right lines?
Thanks in advance.
--
David Osborne
http://www.davidosbornephotography.co.uk/

Sys info
Gallery version = 2.2.4 core 1.2.0.6
PHP version = 4.4.4 cgi
Webserver = Apache/1.3.39 (Unix) mod_auth_passthrough/1.8 mod_log_bytes/1.2 mod_bwlimited/1.4 FrontPage/5.0.2.2635.SR1.2 mod_ssl/2.8.30 OpenSSL/0.9.7a PHP-CGI/0.1b
Database = mysql 4.1.22-standard, lock.system=flock
Toolkits = Exif, LinkItemToolkit, ImageMagick
Acceleration = none, none
Operating system = Linux gator371.hostgator.com 2.6.22_hg_grsec_pax #45 SMP Thu Dec 6 03:29:19 CST 2007 i686
Default theme = carbon
gettext = enabled
Locale = en_GB
Browser = Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.11;MEGAUPLOAD 1.0
Rows in GalleryAccessMap table = 76
Rows in GalleryAccessSubscriberMap table = 838
Rows in GalleryUser table = 4
Rows in GalleryItem table = 318
Rows in GalleryAlbumItem table = 28
Rows in GalleryCacheMap table = 0

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Sun, 2008-02-10 17:36

mod_rewrite won't help here since you want to redirect to a URL that you don't know. in mod_rewrite rules, you can't translate a itemId into a item filename path. you need gallery's database for that.

unless you compile a list of all your items with their filename path and put that into your .htaccess mod_rewrite rules.

so unless you just want to do this for 10 items, the normal approach would be to add this logic / redirect into gallery.
and gallery would have to check the original request-uri and not the rewritten one.

there's no obvious way to add this to g2. i guess i'd add this functionality in modules/core/ShowItem.inc. after the permission check in the code, check if the page has been accessed through g2_item=... instead of with the v/.... short URL. if so, let the view, generate the view URL and return a redirect to the new URL.

--------------
Documentation: Support / Troubleshooting | Installation, Upgrade, Configuration and Usage

 
daveozzz
daveozzz's picture

Joined: 2006-07-25
Posts: 70
Posted: Sun, 2008-02-10 18:00

Thanks very much for the reply.
That all makes sense but one thing that confuses me (probably a misunderstanding of how mod_rewrite works on my part)...

Quote:
in mod_rewrite rules, you can't translate a itemId into a item filename path.

So how does it work the other way, i.e. the URL with a item filename path displays a specific itemId? Isn't it mod_rewrite what does that part?

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Sun, 2008-02-10 22:42

mod rewrite:
v/some/image.jpg.html is translated to main.php?g2_path=some/image.jpg.html.
that's a simple rewrite rule. it looks for the text after v/ and appends it to g2_path=.

and then some logic in g2 looks up the itemId that matches the path and adds the itemId to the request parameters such that the core.ShowItem view knows what item to show.

so you could indeed add a g2 module that registers a rewrite rule for view=core.ShowItem&g2_itemId=... and your module could then translate the id to a path.
but what you can't do from that hypothetical module is to redirect the request. sure you could just add the HTTP location redirect header and exit G2, but that's bad practice and could break other plugins (e.g. if some plugin wanted to do logging). and it's error prone in case you forget to do permission checking.

thus i'd recommend to do this hack directly in the ShowItem view class.

--------------
Documentation: Support / Troubleshooting | Installation, Upgrade, Configuration and Usage

 
daveozzz
daveozzz's picture

Joined: 2006-07-25
Posts: 70
Posted: Mon, 2008-02-11 08:27

Ah ok - that makes a lot more sense now. One to add to my to-do list... thanks again for the information.

 
daveozzz
daveozzz's picture

Joined: 2006-07-25
Posts: 70
Posted: Thu, 2008-02-14 15:05
Quote:
some logic in g2 looks up the itemId that matches the path and adds the itemId to the request parameters such that the core.ShowItem view knows what item to show.

Any chance you could point me at the code that does this part?

 
daveozzz
daveozzz's picture

Joined: 2006-07-25
Posts: 70
Posted: Thu, 2008-02-14 15:32

Actually I've found that now.... what would be good is if I could find the code that builds the g2_path given a g2_itemId (I'm presuming there must be some logic like this to build the links once you active the URL Rewrite module?).

 
daveozzz
daveozzz's picture

Joined: 2006-07-25
Posts: 70
Posted: Thu, 2008-02-14 17:25

OK got it working in a hacky sort of way on my local server.

I've put it in ShowItem.inc. I'm sure there must be a better way (and better place within the file) since this just jumps straight into a redirect and dumps G2. Seems to work though. Any suggestions?

Thanks for the help..

    function loadTemplate(&$template, &$form) {
	global $gallery;
	list ($ret, $item, $wasSpecified) = $this->getItem(true);
	if ($ret) {
	    return array($ret, null);
	}

	if (isset($_GET[GalleryUtilities::prefixFormVariable('path')]) && !$wasSpecified) {
	    /*
	     * Bug #1468797
	     * Detect use of rewritten URL but rewrite module inactive; walk up to find main.php
	     */
	    $urlGenerator =& $gallery->getUrlGenerator();
	    $redirect = dirname($urlGenerator->getCurrentUrlDir()) . '/' . GALLERY_MAIN_PHP;
	    return array(null, array('redirectUrl' => $redirect));
	}

/*** ADDED code ***/
	$urlvars = GalleryUtilities::getUrlVariablesFiltered();
	if  (!isset($urlvars['g2_path']) && isset($urlvars['g2_itemId'])) {
	$itemid = $urlvars['g2_itemId'];
	$urlGenerator =& $gallery->getUrlGenerator();
	$loc = $urlGenerator->generateUrl(array('view' => 'core.ShowItem', 'itemId' => $itemid), array('forceServerRelativeUrl' => true));
	if ($loc != "/gallery2/main.php") {
		$SERVER_NAME = $_SERVER["SERVER_NAME"];

		$redirectpathname = $SERVER_NAME . $loc;

		Header( "HTTP/1.1 301 Moved Permanently" );
		Header( "Location: $redirectpathname" );
		}			
	}