Automatically redirect old pretty URLs

General project discussion. NOT for help questions.
Post Reply
fallingleaf
New Member
New Member
Posts: 4
Joined: Sat Feb 09, 2008 4:20 pm

Automatically redirect old pretty URLs

Post by fallingleaf »

Hi.  It's my first post. I've been using the forum as a resource for a while to help develop my website, but I'm the kind of guy that likes to figure things out for himself.  I'd like to try to participate more, though.  I love CMSMS!

I'm using pretty URLs and I want to change the hierarchy of my site but I want the old URLs to redirect so I don't break old links and Google searches.  Is there some way to do this automatically with a module or a hack?

Thanks,
Noble
Vin

Re: Automatically redirect old pretty URLs

Post by Vin »

Try MovedPages (it's in the module manager). Never tried it myself, though.

[edit] All right, just tried it. It's not automatically. Currently, this module provides just custom page redirect.

BUT, I searched little more... and I found it. In one post, Calguy mentioned that he wrote some User Defined Tag (UDT), and that he posted it. So I looked at User Defined Tags, and here it is:
http://wiki.cmsmadesimple.org/index.php/Share_your_tags_here#A_Redirect_UDT


Sorry, it doesn't seem to be automatic either...

[edit] However, given this UDT, there is some chance to make the redirecting automatically based on the page's change... I'll look into it further...
Last edited by Vin on Sat Feb 09, 2008 6:06 pm, edited 1 time in total.
fallingleaf
New Member
New Member
Posts: 4
Joined: Sat Feb 09, 2008 4:20 pm

Re: Automatically redirect old pretty URLs

Post by fallingleaf »

Thanks for the help.  I don't know why I couldn't find this stuff in my searches.  The UDT looks promising but I notice that this line of the code seems to be missing the right-hand side of the expression:

Code: Select all

        if ($content->GetURL() != )
It's not obvious to me what that should be.

[edit] went to the original forum post from Calguy and it has the right code.
Last edited by fallingleaf on Sat Feb 09, 2008 8:24 pm, edited 1 time in total.
nivekiam

Re: Automatically redirect old pretty URLs

Post by nivekiam »

Stupid wiki formatting....  I've fixed it so it shows '' and not an italicized )
fallingleaf
New Member
New Member
Posts: 4
Joined: Sat Feb 09, 2008 4:20 pm

Re: Automatically redirect old pretty URLs

Post by fallingleaf »

It appears that pretty URLs ignores everything in the URL except what comes after the last "/", which is the page alias.  Since I'm not changing the page aliases, pretty URLs will still find the pages at the old URLs.  What I need to do then is check that the full path in the URL matches the path that is stored in the DB for that page alias, and if not, redirect it to the right path.  If somebody knows an easy way  to do that, I'd be grateful.
Vin

Re: Automatically redirect old pretty URLs

Post by Vin »

For now, you can use the module MovedPages, this one is better in handling redirects (pretty URLs).

Currently, I'm trying to find a way how to automate the redirection with the help of the Events and MovedPages, but it will take some time.
fallingleaf
New Member
New Member
Posts: 4
Joined: Sat Feb 09, 2008 4:20 pm

Re: Automatically redirect old pretty URLs

Post by fallingleaf »

The documentation for MovedPages is not at all clear to me.  All I could make it do is display a blank page.

What I was thinking is that I can get the URL path from php environment variables and compare that to the field "hierarchy_path" from the content table in the DB.  If these don't match up I can redirect to the value of hierarchy_path using the redirect UDT.  Is there a way to get the value of hierarchy_path from a Smarty template?
Vin

Re: Automatically redirect old pretty URLs

Post by Vin »

fallingleaf wrote: The documentation for MovedPages is not at all clear to me.  All I could make it do is display a blank page.
Yeah, I was a little confused too... well, after doing everything you were told in the help of the module, go to Extensions > MovedPages and add the URLs you want to redirect (the Add moved pages tab).
nivekiam

Re: Automatically redirect old pretty URLs

Post by nivekiam »

Is there a way to get the value of hierarchy_path from a Smarty template?
Sort of, at least in theory.  Or maybe I should just say yes, but I don't know how ;)

I've been digging around.  I've figured out how to construct the hierarchy path, but not how to get it.

If you open up index.php and go down to this section of code

Code: Select all

if ($page == '')
{
        global $gCms;
        $contentops =& $gCms->GetContentOperations();
        $page =& $contentops->GetDefaultContent();
}
then scroll down just a little farther to these two line:

Code: Select all

        $smarty->assign('position', $pageinfo->content_hierarchy);
        $smarty->assign('friendly_position', $gCms->variables['friendly_position']);
Then add these lines below those two above (note, I've hard coded the content ID 00002 for testing)

Code: Select all

echo $page;
echo '<br>';
echo $pageinfo->content_id;
echo '<br>';
echo $pageinfo->content_alias;
echo '<br>';
echo $pageinfo->content_hierarchy;
echo '<br>';
echo $gCms->variables['friendly_position'];
echo '<br>';
echo $contentops->GetPageAliasFromID($pageinfo->content_id);
echo '<br>';
echo $contentops->GetPageIDFromHierarchy('00002');
echo '<br>';
echo $contentops->GetPageIDFromHierarchy('00002.00002');
echo '<br>';
echo $contentops->GetPageAliasFromID($contentops->GetPageIDFromHierarchy('00002')) . '/' . $contentops->GetPageAliasFromID($contentops->GetPageIDFromHierarchy('00002.00002')) . '/' . $page;
echo '<br>';
echo $_SERVER['REQUEST_URI'];
echo '<br>';
echo $_SERVER['PHP_SELF'];
echo '<br>';
echo substr($_SERVER['PHP_SELF'], strrpos($_SERVER['PHP_SELF'], '/') + 1);
Then I've found the file /lib/classes/class.content.inc.php it looks like it has a lot of very useful functions and information, it even looks like someone has already created functions that might get us close to what we want, URL verification.  But I have no idea how to utilize that stuff.

Now it's time for the serious programmers to laugh at me ;)

I've tried $contentinfo =& $gCms->GetContentBase(); and trying to extract info from there like so:
$hierarchy =& $contentinfo->HierarchyPath();

That returned nothing, IIRC.  Then I tried various other things that all generated errors.  I'm just hitting that wall of not knowing OOP.
Vin

Re: Automatically redirect old pretty URLs

Post by Vin »

nivekiam wrote: Then I've found the file /lib/classes/class.content.inc.php it looks like it has a lot of very useful functions and information, it even looks like someone has already created functions that might get us close to what we want, URL verification.  But I have no idea how to utilize that stuff.

Now it's time for the serious programmers to laugh at me ;)

I've tried $contentinfo =& $gCms->GetContentBase(); and trying to extract info from there like so:
$hierarchy =& $contentinfo->HierarchyPath();

That returned nothing, IIRC.  Then I tried various other things that all generated errors.  I'm just hitting that wall of not knowing OOP.

That's funny... I was going to post similar post, as I had to dig up some core functions...
/lib/classes/class.content.inc.php definitely, you're right... I need to call the GetURL() function somehow, but I have no idea how to call it and use the member variables from the params['content'], which is sent by ContentEditPre event... guess I should RTFM of STFW...
Last edited by Vin on Sun Feb 10, 2008 6:44 pm, edited 1 time in total.
nivekiam

Re: Automatically redirect old pretty URLs

Post by nivekiam »

Possible solution.  I put this in my index.php:

Code: Select all

        $content = new ContentBase();
        $content->LoadFromId($pageinfo->content_id);
        $smarty->assign('hierarchy_path', $content->GetUrl());
That will create a smarty variable named hierarchy_path that you should be able to use in your templates.  Personally, I won't be using the smarty variable.  I'll be using the info from GetUrl() and comparing that to the current URL someone is at.

I think I'll use this function to check the current URL:

Code: Select all

function curPageURL() {
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pageURL;
}
Then compare that to what it is suppose to be.  If it's not correct, then I'll either do a 301 redirect or send a 404 error and have a custom, very friendly page with something that mentions, "Is this the page you meant to go to.", with a link to the correct page.  It would all depend on what you are doing.  I'm not restructuring my site so I'd send a 404 error, if you're restructuring, I'd probably send the 301 redirect to tell Google and browsers what the intention is.

I haven't decided if I'll try to do this with a module or plugin or if it can be done that way.  I'll probably just hack the index.php and config.php files so I can have the option of turning this off or on quickly.

[EDIT]
Forgot this line of thought.  I don't know how expensive this operation is.  It does add 1 database call, but in terms of processing I have no idea how much it costs, or if it's even the correct way to do it.  The LoadFromId function has 2 arguments you can pass it, the page id and whether to load properties or not, which is false by default.  I honestly don't know what that does.

Code: Select all

    /**
     * Load the content of the object from an ID
     *
     * @param $id               the ID of the element
     * @param $loadProperties   whether to load or not the properties
     *
     * @returns bool            If it fails, the object comes back to initial values and returns FALSE
     *                          If everything goes well, it returns TRUE
     */
        function LoadFromId($id, $loadProperties = false)
There is also this comment at the top of class.content.inc.php

Code: Select all

 * As for some treatment we don't need the extra properties of the content
 * we load them only when required. However, each function which makes use
 * of extra properties should first test if the properties object exist.
So there might be some extra error checking that should happen before we do any of this, I don't know how to do that.
Last edited by nivekiam on Mon Feb 11, 2008 2:22 am, edited 1 time in total.
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm

Re: Automatically redirect old pretty URLs

Post by calguy1000 »

I'll pipe in here a bit.

I don't think there's a way you can make this automatic.  Especially to do a 301 redirect.
You do need a way of knowing in the old page, what the new page is.

so when moving pages around, you could just copy the page (yeah, I know, there's no copy page feature yet)... and then in the old page put some code that generates a 301.

Or there's the movedpages module that's supposed to do this (I've never tried it, but I may check it out).

for 404, CMS has a form built into this in the global settings page.
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
User avatar
tinhat
Forum Members
Forum Members
Posts: 65
Joined: Fri May 23, 2008 6:33 am

Re: Automatically redirect old pretty URLs

Post by tinhat »

This is a good argument for *not* using hierarchy in pretty URLs, IMHO. For SEO, if your user reorganises their site then the internal navigation link structure will be different from whatever link structure google already has and more importantly from what might be in external links pointing to pages in the site. I agree that plain ol' hand coded 301 redirects in .htaccess is the best cure. You might want to think about turning use hierarchy off in your config to avoid future problems?
Vin

Re: Automatically redirect old pretty URLs

Post by Vin »

Finally found the solution!
calguy1000 wrote: I don't think there's a way you can make this automatic.  Especially to do a 301 redirect.
You do need a way of knowing in the old page, what the new page is.
I can :-). The trick is using ContentEditPost and ContentDeletePost event together with MovedPages module. I managed to make a mechanism which creates and modifies redirects and permanent removals every time the content is edited/deleted/added. It has still a few glitches, though - that AJAX apply button doesn't seem to update the mHierarchyPath.

I will post the code in the SVN, when I figure out how. I would like to have the Moved Pages included in the CMSMS package, by the way. I think this a common feature which anybody can use - just like some backup module.
calguy1000 wrote: for 404, CMS has a form built into this in the global settings page.
Sure, but 404 is not 410 - Permanently Removed.
Post Reply

Return to “General Discussion”