1

I'm using tje AheadWorks blog extension and the URL for a post when linked from the main blog page vs a category page is different.

For example, from the main blog page:

domain.com/blog/blog-post-title

From a category page:

domain.com/blog/cat/category-name/blog-post-title

This obviously creates duplicate content issues.

I would like to force all URLs to be the same as from the main blog page, i.e.

domain.com/blog/blog-post-title

I don't know enough about Magento to try any options, otherwise I would have posted what I've already done.

Any advise would be appreciated.

  • Leaving this question open as it can likely be answered in a broadly applicable way. – benmarks Jul 26 '16 at 21:35
1

This can simply be done from the Settings menu of the plugin. Set "Use Categories ID in Posts URLs" to "No enter image description here

  • This is the best answer to the question. No programming needed. – Nathan Dec 23 '16 at 15:59
0

There's a couple of things you could do. There is some code required since this module doesn't support things like canonical URL's. Magento in fact does for catalog and CMS pages. I wrote up some solutions for you to use, these are:

  1. Adding a canonical URL to the head
  2. Change the custom router Controller to redirect to the non-category URL of the post
  3. Change template files so it shows the URL of the non-category post.

If you're problem is duplicate content, you should add a canonical URL to your HTML head. A canonical URL element tells the search engine what is the original URL. In this way, you won't get penalty's for duplicate content. This is the best way to go, with the least effort. If you don't want your visitors to land on the category url of your post, you could implement option 2 and or 3 also, but they are not necessary and a bit sloppy, but it gets the job done.

A canonical URL looks like this:

<link rel="canonical" href="http://bfd.dev/blog/tesblog/">

Adding the Canonical URL

For this to work, we need a function that generates the canonical URL, and specify directives within layout XML to add it to the HTML head. We will add our generating function to the data helper of the blog module. It is located at app/code/community/AW/Blog/Helper/Data.php

Somewhere in this file, within the class (before last bracket) add the following code:

 /**
 * Generates the canonical URL
 * @return string
 */
public function getCanonicalUrl(){
    $route = $this->getRoute();
    $id = end(array_filter(explode('/',Mage::app()->getRequest()->getRequestUri())));
    $url = Mage::getUrl($route.'/'.$id);
    return $url;
}

This function fetches the $route (blog by default or whatever you specify in the backend). Then chops the requested URL in pieces by the / within the explode() function. It filters out empty array elements using array_filter() and takes the last part of our array using end(). This would be your blog post identifier.

Then we use Mage::getUrl() to create our canonical URL. If some user here on stack knows a better way, please let me know!

Adding our Canonical URL to our HTML head using Layout XML

Now we have our URL, we will add it to our HTML head. We use Layout XML for this. Depending on the theme you are using. You have to edit or copy the file to your theme directory. My development store runs on the RWD / default theme so my file location is:app/design/frontend/rwd/default/layout/aw_blog.xml

If you're not known with Magento's theme / layout concepts, let me know. I'll provide some extra documentation.

Within the aw_blog.xml file, look for <blog_post_view> Underneath that (around line 61), add the following code:

    <reference name="head">
        <action method="addLinkRel">
            <rel>canonical</rel>
            <href helper="blog/getCanonicalUrl"></href>
        </action>
    </reference>

What this means is: within the <head> reference, we want to execute the action that adds a <link ... element with a rel= part that equals to canonical and the href= content should be fetched from our blog helper using our new getCanonicalUrl() function. It should look like this.

 <blog_post_view>
    <reference name="head">
        <action method="addLinkRel">
            <rel>canonical</rel>
            <href helper="blog/getCanonicalUrl"></href>
        </action>
    </reference>
    <reference name="footer" >
        <block type="blog/blog" name="copyright.blog.footer" template="aw_blog/copyright.phtml"/>
    </reference>
    <reference name="content">
        <block type="blog/post" name="post" template="aw_blog/post.phtml">
            <block type="blog/product_toolbar" name="aw_blog_list_toolbar" template="catalog/product/list/toolbar.phtml">
                <block type="blog/html_pager" name="product_list_toolbar_pager"/>
            </block>
        </block>
    </reference>
</blog_post_view>

Now, when you load your blog post, it should have the canonical URL to make sure we won't get penalty's for duplicate content. If it isn't showing up. Make sure you modified the right file, the names are correct and that you have flushed your cache.

Modifying the custom router

You should definitely add the canonical element. If you don't want your visitors to get the category based url of your blog post, we could modify AW's custom router to redirect it to the original (also canonical) URL.

At file: app/code/community/AW/Blog/Controller/Router.php around line 135 you'll see this part:

            if (isset($postident)) {
                $post = Mage::getSingleton('blog/post');
                if (!$post->load($postident)->getId()) {
                    return false;
                }


                $request->setModuleName('blog')
                    ->setControllerName('post')
                    ->setActionName('view')
                    ->setParam('identifier', $postident)
                    ->setParam('cat', $identifier);
                return true;
            } else {

In here, remove the code starting at $request->setModuleName('blog') until the return true; line. And swap it with:

                $route = $helper->getRoute();
                $id = $postident;
                $url = Mage::getUrl($route.'/'.$id);

                Mage::app()->getFrontController()->getResponse()
                    ->setRedirect($url)
                    ->sendResponse();
                exit;

It will make your visitor, redirect to the original url. If you getting error reports, Front controller reached 100 router match iterations there is something wrong with the change you made here, or my code.

Alternatively, you could modify the <a href= tags within the templates. But i think the canonical option is sufficient. I have to go right now so i don't have time to add this too. If you require more assistance let me know and i'll respond within a couple of hours. Goodluck!

p.s. Make sure to test that everything works as expected, it works for me, but it was only tested quickly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.