URL rewrite: hierarchical urls?

alecmyers

Joined: 2006-08-01
Posts: 4342
Posted: Mon, 2007-12-24 17:41

I'd like to have urls of the following formats

/favourites
/favourites/275
/favourites/275/3

all rewritten, respectively, as follows:

?g2_view=favourites.FavouritesAlbum
?g2_view=favourites.FavouritesAlbum&g2_targetUserId=275
?g2_view=favourites.FavouritesAlbum&g2_targetuserId=275&g2_page=3

My best attempt at coding this in module.inc is this:

    function getRewriteRules() {
	return array(
      		array('comment' => $this->translate('Favorites Album'),
		      'match' => array('view' => 'favourites.FavouritesAlbum'),
		      'pattern' => 'favourites',
		      'help' => $this->translate('Shortest URL for album of Favorite Items')
		      ),
		      
		array('comment' => $this->translate('Favorites Album'),
		      'match' => array('view' => 'favourites.FavouritesAlbum'),
		      'pattern' => 'favourites/%targetUserId%',
		      'keywords' => array(	'targetUserId' => array( 'pattern' => '([0-9]+)$', 'ignored' => 0 )	),
		      'help' => $this->translate('Shorter URL for album of Favorite Items')
		      ) ,

		array('comment' => $this->translate('Favorites Album'),
		      'match' => array('view' => 'favourites.FavouritesAlbum'),
		      'pattern' => 'favourites/%targetUserId%/%page%',
		      'keywords' => array(	'targetUserId' => array( 'pattern' => '([0-9]+)', 'ignored' => 0 ),
		      				'page' => array(   'pattern' => '([0-9]+)', 'ignored' => 0 ) 	),
		      'help' => $this->translate('Short URL for album of Favorite Items')
		      )
	
		
		     );
    }

In other words to register three different rewrite rules, one for each possibility. The results of this are consistent only in that enabling all three rules doesn't correctly rewrite urls of all three formats, although where it fails exactly appears to depend on other factors like in which order the rules are enabled.

Is it in fact possible to rewrite all three url possibilities for a single view?

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Tue, 2007-12-25 01:53

what code do you get in .htaccess?

 
alecmyers

Joined: 2006-08-01
Posts: 4342
Posted: Tue, 2007-12-25 03:14

In .htaccess I have the following sets of entries (not adjacent, the first was separated from the second two and I've redacted the intervening lines)

  RewriteCond %{THE_REQUEST} /favourites(\?.|\ .)
    RewriteCond %{REQUEST_URI} !/main\.php$
    RewriteRule .   /main.php?g2_view=favourites.FavouritesAlbum   [QSA,L]
      .
      .
     <other entries here>
      .
      .
    RewriteCond %{THE_REQUEST} /favourites/([0-9]+)/([0-9]+)(\?.|\ .)
    RewriteCond %{REQUEST_URI} !/main\.php$
    RewriteRule .   /main.php?g2_view=favourites.FavouritesAlbum&g2_targetUserId=%1&g2_page=%2   [QSA,L]
    RewriteCond %{THE_REQUEST} /favourites/([0-9]+)$(\?.|\ .)
    RewriteCond %{REQUEST_URI} !/main\.php$
    RewriteRule .   /main.php?g2_view=favourites.FavouritesAlbum&g2_targetUserId=%1   [QSA,L]

Also the order here is not the same as the order of the entries in module.inc, which seems odd.

 
alecmyers

Joined: 2006-08-01
Posts: 4342
Posted: Tue, 2007-12-25 03:26

Instead of trying to get all three url formats working, I can (sort of) get just two working:

/favourites
/favourites/<userid>

by removing the $ from pattern field in the code, and activating two of the rewrite options in the admin page.

Manually typing those two format urls sees them correctly rewritten by the entries in .htaccess which takes you to the correct page - but they're not tranlated the other way, into the html, which appears with "mixed" urls of the format

/favourites/g2_targetUserId=<xxx>

i.e. it's only using the simpler of the two active rewrite rules.

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Tue, 2007-12-25 04:00

yeah, the $ looks definitely wrong there. remove it from your pattern.
else the htaccess looks fine.

@generation of URLs:
yeah, it's probably not ready for multiple short-urls per view.
i'd have to review the rewrite code, but it's probably lacking the necessary iteration over all candidate short-urls to pick the one with the greatest common params.

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