4

I have been at this for sometime now. I have read article after article and watched videos on youtube and I STILL can't figure this out. So I am asking for help on explaining how to use URL Rewrites with Magento and what I am doing wrong.

Here is what I have and what I am looking to do. Could you please describe to me in detail what I need to do?

System > Configure > Web Settings

Add Store Code to Urls = No
Auto-redirect to Base URL = No
Use Web Server Rewrites = Yes
  1. Navigational Bar

    In my navigational bar I have a link called "Cost of Savings" . Nav path WHY ERG > Cost Of Savings

    Name = Cost of Savings Comparison
    URL Key = cos
    
  2. CMS Page

    I have a CMS Page created CMS > Pages > Cost of Savings Comparison

    Name = Cost of Savings Comparison
    URL Key = cos
    
  3. URL Rewrite System Settings for cos

    Type = System
    ID Path = category/41
    Request Path = about-erg/cos.html
    Target Path = catalog/category/view/id/41
    Redirect = No
    
  4. Custom URL (I Created)

    Type = Custom
    ID Path = category/41
    Request Path = about-erg/cos.html
    Target Path = cos
    Redirect = No
    
  5. What I am doing

    1. I go in to URL rewrites and I create the Custom rewrite using the info in step 4. Then before I save the new custom rewrite I delete the system rewrite magento created (step 3) because if I try to save the custom one it will error while the system one is present. After the system one has been deleted I then I save the new custom rewrite I created (step 4).

    2. Once I do this I go to the front-end and refresh the site then click on Why ERG > Cost of Savings and the pages loads wonderfully. But if I go to Magento's Index Managment (system > index management) and re-index tables then go back to the front-end and refresh the site the Cost of Savings doesn't work any more and Magento replaces my deleted System URL rewrite (step 3).

Can someone please tell me what I am doing wrong and how do I get it so this doesn't happen and the Cost of Savings link works?

Please let me know if you have any questions. Thank you VERY very Much!!

Frank G.

  • If I read this right, you are trying to set a category URL to be the same as a CMS page URL? I don't think you can do that. – Joseph Leedy Jul 28 '13 at 20:18
  • Hi @Joseph, please post your comment as an answer as it really sounds like this. Maybe you can also enhance the answer by providing tips about how to add a link to CMS page into main navigation. – user487772 Jul 28 '13 at 21:18
  • @Joseph, thanks for your reply. Yes I have a menu item in the nav bar called Cost of Savings and I want to link that to a CMS Page called Cost of Savings. If you can't do that then how do you create page links within the menu bar of Magento? – Frank G. Jul 28 '13 at 21:20
2

If all you are wanting is to have an item in your main navigation that links to a CMS page without having to hardcode it into the navigation template or include it via a static block then you can use this guide here -> http://jc-designs.net/blog/2012/09/magento-adding-cms-pages-to-the-main-navigation/

There are a few out there but I always use this one (as I can never remember how to do it even after doing it umpteen times). Yes, it is ludicrously convoluted.

  • thank you for your response. I think this might be a more fitting solution to the problem. But Magento really should revist how they handle links in the nav and not make it so hard to do such a simple task. I am going to try this tutorial step by step and see how it goes. If I have any problems or questions I will let you know. – Frank G. Jul 29 '13 at 19:29
  • OK - I like Marius' answer and have upvoted it but for me writing a whole module for such a simple thing is just OTT. I like this way, but yearn for an equivalent of Wordpress wp_nav_menus! The bonus of this is that, unlike template changes and static block links, Magento will apply the 'active' class to it when selected, so much better for styling. – McNab Jul 29 '13 at 19:34
  • thankfully it worked like a charm!! Thank you! – Frank G. Jul 30 '13 at 22:15
  • Excellent! Don't forget to accept the answer if it solved your problem - meta.stackexchange.com/questions/5234/… ;) – McNab Jul 31 '13 at 8:11
  • I can't it wont let me. I don't have enough rep :( Sorry! – Frank G. Aug 1 '13 at 4:43
3

The best approach on this is to make the category display a static block and create a static block with the contents of your current cms page. and avoid all this url rewrite 's...tuff'.
An other approach would be to include the link of your cms page in the menu. Starting with Magento CE 1.7 there is an event that lets you add anything to the top menu page_block_html_topmenu_gethtml_before. See more details about it here.
The event above will allow you to put the link to your page at the beginning or the end of the menu. You cannot insert a link between the categories.
If you want to insert the link between some categories this might work: Have a category (like you do now) and hook on the predispatch event of the category view page to tell magento that for that specific category you want to display a cms page.
For this you will need a custom module. Let's call it Easylife_Redirects with the following files: app/etc/modules/Easylife_Redirects.xml - the declaration file

<?xml version="1.0"?>
<config>
    <modules>
        <Easylife_Redirects>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Core />
            </depends>
        </Easylife_Redirects>
    </modules>
</config>

app/code/local/Easylife/Redirects/etc/config.xml - the configuration file

<?xml version="1.0"?>
<config>
    <modules>
        <Easylife_Redirects>
            <version>0.0.1</version>
        </Easylife_Redirects>
    </modules>
    <global>
        <models><!-- declare extension models alias -->
            <redirects>
                <class>Easylife_Redirects_Model</class>
            </redirects>
        </models>
    </global>
    <frontend>
        <events>
            <controller_action_predispatch_catalog_category_view><-- hook on the predispatch of the category view page-->
                <observers>
                    <redirects>
                        <class>redirects/observer</class>
                        <method>redirectToPage</method>
                    </redirects>
                </observers>
            </controller_action_predispatch_catalog_category_view>
        </events>
    </frontend>
</config>

app/code/local/Easylife/Redirects/Model/Observer.php - the observer
Let's assume you category id is 41 (like your example) and the id of the CMS page is 3

<?php
class Easylife_Redirects_Model_Observer{
    function redirectToPage($observer){
        if (Mage::app()->getRequest()->getParam('id') == 41){//if on the specified category
            //I like naming my exceptions `up` before throwing them :)
            $up = new Mage_Core_Controller_Varien_Exception();
            //prepare the forward to the cms page - The magento router will take care of the rest. 
            //The forward is done to the `view` action, from the `page` controller in the `cms` module. `array('page_id'=>3)` are the additional GET parameters.
            $up->prepareForward('view', 'page', 'cms', array('page_id'=>3));
            throw $up;
        }
        return $this;
    }
}
  • I'm not sure that's such a good idea. The power of Magento is the CMS and turning that over to a client to be able to create a page and links in the nav bar on the fly without me. If I have to create and hard code a page in then that defeats the purpose of CMS then and the client really doesn't have the ability to do it themselves. That's why I wanted to figure out how to do it with the rewrites so I can pass that on to the client. I do appreciate your effect and help thank you! – Frank G. Jul 29 '13 at 19:27
  • I actually wrote this code as a proof of concept. I really don't like hard-coding values neither. Nothing prevents you from creating a module or an attribute on a category called for example "Display static page" as a dropdown with all the pages, and with the use of this observer, display the appropriate cms page. – Marius Jul 29 '13 at 19:33

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.