Joomla 1.7 - How do I create an SEF link to an article?

1,374 views
Skip to first unread message

Nick Weavers

unread,
Dec 10, 2011, 7:48:01 AM12/10/11
to Joomla! CMS Development
I have developed a template for a client and want to put some links in
it to articles they created. I have enable both the SEF options
(internal sef and mod_rewrite) in the site configuration.

At the moment I am creating the links like this:

<?php
$url = "index.php?option=com_content&view=article&id=38-
remortgage";
$sef_url = JRoute::_( $url, $xhtml = true, $ssl=null );
?>
<a href="<?php echo $sef_url; ?>">Remortgage</a>

The article does appear when I click on the link, but the URL in the
browser address shows as:

http://<my_domain>/component/content/article?id=38-remortgage

I would like to get it to appear something like:

http://<my_domain>/component/content/article/38-remortgage

Can anyone tell me what I'm doing wrong that is causing the ?id=38-
remortgage query string to appear instead of it being pushed into the
path?

Many thanks,
Nick

Hannes Papenberg

unread,
Dec 10, 2011, 8:37:45 AM12/10/11
to joomla-...@googlegroups.com
This is actually a bug in Joomla since 1.5, which is not being
acknowledged as a bug by the PLT. To get around this in your PHP code,
you need to use ContentHelperRoute::getArticleRoute($item->id,
$item->catid) inside of the JRoute instead of the $url. If you create a
link in your content (so, simply as pure HTML), it will not work and you
have to manually pick out the correct Itemid to use instead.

I filed two tracker items before 1.6 was released as stable, but they
were closed without a comment. I also proposed a rewrite of the routing
system which would have fixed this for 2.5, but it was also rejected.

Hannes

fornandakishore

unread,
Dec 10, 2011, 8:01:27 AM12/10/11
to joomla-...@googlegroups.com
Hello Nick,

Here is my Idea...

1. Create a Menu link for that article ID, use title alias for the menu like "
remortgage"

2. add &Itemid=xx(created menu id) to the URL

     
$url = "index.php?option=com_content&view=article&id=38-remortgage&Itemid=xx";

3. That's it, now your url is something like http://www.urdomain.com/remortgage.

Hope this will help you...
 
---------------------

Nanda Kishore. M

PHP Developer

http://php-desk.blogspot.com

Ph: + 91 98499 71144 (Cell)





--
You received this message because you are subscribed to the Google Groups "Joomla! CMS Development" group.
To post to this group, send an email to joomla-...@googlegroups.com.
To unsubscribe from this group, send email to joomla-dev-cm...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/joomla-dev-cms?hl=en-GB.


Nick Weavers

unread,
Dec 10, 2011, 11:50:14 AM12/10/11
to Joomla! CMS Development
Hi Hannes,

Thanks for your explanation and for helping. I changed the code as you
advised:

<?php
$url = ContentHelperRoute::getArticleRoute('5', '7');
//$url = "index.php?option=com_content&view=article&id=5-about-
us";


$sef_url = JRoute::_( $url, $xhtml = true, $ssl=null );
?>

<a href="<?php echo $sef_url; ?>">about us</a>

This time I see the value of $url become "index.php?
option=com_content&view=article&id=5&catid=7"
and $sef_url becomes "/component/content/article/7-about-us/5-about-
us".

If I revert back to the hard coded html link, but this time include
the catid and use just the id for the article and not its "slug", (ie
the form returned by ContentHelperRoute::getArticleRoute('5', '7')),
it gets transformed correctly (perhaps not surprisingly) by JRoute::_
as follows:

<?php
//$url = ContentHelperRoute::getArticleRoute('5', '7');
$url = "index.php?option=com_content&view=article&id=5&catid=7";


$sef_url = JRoute::_( $url, $xhtml = true, $ssl=null );
?>

<a href="<?php echo $sef_url; ?>">about us</a>

Since all articles have a unique id (ie their primary index) why does
the com_content router not deal with just an article id or slug
correctly. Is this the bug you refer to?

Again, thanks.
Nick

Hannes Papenberg

unread,
Dec 10, 2011, 12:03:06 PM12/10/11
to joomla-...@googlegroups.com
No, I'm referring to the bug that the Itemid is not properly handled.
Since the ContentHelperRoute::getArticleRoute() also returned a link
without an Itemid, I can say that your article is not linked in the menu
either directly or indirectly via a category.

Hannes

Nick Weavers

unread,
Dec 10, 2011, 12:10:12 PM12/10/11
to Joomla! CMS Development
Hi Fornandakishore,

Many thanks for taking the time to give your suggestion, I really
appreciate it. I did try this as well, but ultimately I will be
building hundreds of these links for selection in a custom module (My
clients SEO company want this). I didn't want to complicate this post
with those details.

Regards,
Nick

Nick Weavers

unread,
Dec 10, 2011, 12:24:57 PM12/10/11
to Joomla! CMS Development
No, it isn't. Is this a requirement for it to work?

Out of interest, can you tell me why the com_content router doesn't
allow just an article id to be given? The content parser seems to have
code in it that would suggest it was intended to handle this
situation.

// if there is only one segment, then it points to either an article
or a category
// we test it first to see if it is a category. If the id and alias
match a category
// then we assume it is a category. If they don't we assume it is an
article


I don't plan for there to be a menu item ever. The intention is that
articles links will appear on most/all pages via a module and I want
the URL to be as short an SEF URL as possible as the articles are
purely for SEO and as we know, if an article can be made to look like
it is a page in the root directory Google et-al will tend to give it
higher weighting than one apparently in a sub directory or below.

Thanks,
Nick


Reply all
Reply to author
Forward
0 new messages