Page 1 of 1

[solved] Mod_rewrite Pretty URLs and Child Content Available From Any Parent

Posted: Wed Jul 22, 2009 8:07 pm
by davidkirk451
We're using Mod_rewrite and pretty urls, which works great.  The problem is, all the children pages are available anywhere.  For instance I can get to page1.htm from http://domain/page1.htm, http://domain/correctparent/page1.htm, http://domain/anyotherparent/page1.htm.

How would I set it up so all pages need the correct parent specified?  Is there an option so that you have to specify the parent on the actual URL (http://domainname/index.php?page=pagena ... parentname).

Thanks for any help!

Re: Mod_rewrite Pretty URLs and Child Content Available From Any Parent

Posted: Wed Jul 22, 2009 10:06 pm
by calguy1000
We're using Mod_rewrite and pretty urls, which works great.  The problem is, all the children pages are available anywhere.  For instance I can get to page1.htm from http://domain/page1.htm, http://domain/correctparent/page1.htm, http://domain/anyotherparent/page1.htm.
This is not a problem or a bug in CMSMS... we've gone over this numerous times...
a) Users don't memorize or try to invent urls (except me and a few hackers), they bookmark them.
b) Search engines don't guess urls, they follow them.
c) the canonical stuff that google and other search engines are supporting now solves any duplicated content issues created by supplying numerous links to the same content.

Therefore, no problems.

[solved] Re: Mod_rewrite Pretty URLs and Child Content Available From Any Parent

Posted: Wed Aug 05, 2009 4:34 pm
by davidkirk451
That's all great, but it was still a requirement here.  I now have it working on our site at http://www.c7dc.com/.

This will 301 redirect any page to the proper /parent/child URL (like http://www.c7dc.com/services/colocation.htm) URL, even if you enter it with the actual index.php?page=pagename.  I also added a check for news articles, so it doesn't do the redirection on them.

Here is the UDT that I inserted in all templates on our site to make this work:

Code: Select all

// get current page
global $gCms;
$manager =& $gCms->GetHierarchyManager();
$thisPage = $gCms->variables['page_name'];

if ($thisPage!="")
{
	//get current page parent alias
	$currentNode = & $manager->sureGetNodeByAlias($thisPage);
	$parentNode = $currentNode->getParent();
	if(isset($parentNode))
	{
		$content = $parentNode->getContent();
		$parent_title = $content->MenuText();
		$parent_alias=$content->Alias();
	}
	else
	{
		$content = $currentNode->getContent();
		$parent_alias=$content->Alias();
		$parent_title="";
	}

	//Make available to smarty
	$smarty->assign('parent_title', $parent_title);
	$smarty->assign('parent_alias', $parent_alias);

	preg_match('/^\/(.*)\/(.*\.htm||\/)$/', $_SERVER["REQUEST_URI"], $matches);
	$urlparent=$matches[1];
	$urlpage=$matches[2];
	if (!isset($urlpage))
	{
		preg_match('/\/(.*\.htm|\/)$|/', $_SERVER["REQUEST_URI"], $matches);
		$urlpage=$matches[1];
	}
	if(strcasecmp($urlparent,$parent_alias)!=0 && !preg_match("/news\/\d*\/\d*/", $urlparent))
	{
		// Permanent redirection
		header("HTTP/1.1 301 Moved Permanently");
		header("Location: http://www.c7dc.com/".$parent_alias."/".$urlpage);
	}
}

Re: [solved] Mod_rewrite Pretty URLs and Child Content Available From Any Parent

Posted: Wed Aug 05, 2009 6:53 pm
by Dr.CSS
Nice looking site, but you may want to run it thru the validation before putting the valid html button on site...

http://validator.w3.org/check?uri=http% ... %3Bq%3D0.7

Re: [solved] Mod_rewrite Pretty URLs and Child Content Available From Any Parent

Posted: Thu Aug 06, 2009 7:26 pm
by davidkirk451
Thank you for bringing that to my attention.

Re: [solved] Mod_rewrite Pretty URLs and Child Content Available From Any Parent

Posted: Mon Aug 10, 2009 11:27 am
by Pierre M.
Hello,

does your UDT work for /any/depth/of/path/to/page.html or only for /badpath/page.html ?

Pierre M.

Re: [solved] Mod_rewrite Pretty URLs and Child Content Available From Any Parent

Posted: Wed Feb 24, 2010 3:46 am
by davidkirk451
Nope, just one parent->child level.  I would think you could modify it to do by having it call itself and start with that child page, but you'd have to make sure it didn't get into an infinite loop.