Problems, need help? Have a tip or advice? Post it here.
12 posts Page 1 of 2
Hello..

Have page with name kids-shampoo which results in kids-shampoo.html

Was requested to change page to shampoo-for-kids.

I know how to change the page name but how do I redirect users who still go to kids-shampoo to the new name of shampoo-for-kids?

Since the pagename does not exist, I can't perform a redirect after cms:if k_is_page

Thanks
Hi,

You can handle that by placing a redirection rule in your site's .htaccess file.
Care should be taken, however, to place the rule at the correct location -
Code: Select all
..
..

#DO NOT EDIT BELOW THIS

..
..

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule . - [L]

#####################

RewriteRule ^reviews/new-honda.html http://www.yoursite.com/news/new-honda.html [R=301,L,QSA]

#####################

..

The code above marks out the location for you with a sample redirection rule.
You can place your rule in its place.

Hope this helps.
Thanks for that. This is working for now but I would like to get it so admin's could do this via the panel.

I was thinking that in the globals for each template I could have a repeatable list where admins can place the old pagename and then the new pagename it should redirect to and possibly the redirect code for perm or temporary. Then by using routes on that template maybe I can check the pagename (this is the part i need to figure) before the system 404's it and redirect if that pagename request exists in the repeatable in the globals section of the template.

What do you think?
'Relay' feature? Sounds complicated and therefore tempting. I briefly refreshed the code which takes care of identyfying which page to load depending on provided url (normal default Couch routing). To create a perfect solution done right there are decisions to make -
- if the pages are going to be in the same template or possibly in different
- if redirect is silent or a note visible to user (on 404 page or intermediary template)
- if lookup executes in PHP code or in an Intermediary template in Couch code
- if file .htaccess must also be updated and whether manually or automatically
- if meta tags in HTML are needed for search engines to glue new url and old content
- if correct event is used to hook to in Couch via PHP (there are at least 2 events to wisely choose from, maybe more).
etc, etc.

Very interesting though. :)


P.S. All of the above applies to a potential solution for users without the requirement of using CustomRoutes addon. If you want that to be done via Routing, then I advise to use filter - I had used it previously with success to enforce a redirect to a new page if the language changes in URL. Default example has already a filter - page_exists, so use it for a custom lookup-and-redirect code.
Code: Select all
<cms:route name='page_view' path='{:page_name}{:format}' filters='page_exists=name'>
    <cms:route_constraints
        format='(\.html)'
    />
    <cms:route_validators
        page_name='title_ready'
    />
</cms:route>
Thanks for everyones help, I wrote something quick up but redirects can now be handled within couch admin for the template.

I am sure it could be expanded further and I may but for only spending a short time on it this is what I have. :)

Template code:
Code: Select all
<cms:template title='Your title' clonable='1' order='1' routable='1' >
    <cms:route name='list_view' path='' />
    <cms:route name='page_view' path='{:page_name}{:format}' >
        <cms:route_constraints
            format='(\.html)'
        />
        <cms:route_validators
            page_name='title_ready'
        />
    </cms:route>

    <cms:globals>
        <cms:editable type="dropdown" name="enable_redirects" label='Enable redirects' opt_values="Select=- | Yes=yes | No=no" required='1' order='1' />

        <cms:func _into='redirect_chk' enable_redirects='' >
            <cms:if enable_redirects = 'yes' >show<cms:else />hide</cms:if>
        </cms:func>

            <cms:repeatable name='template_redirects' label='Template redirects' order='1' not_active=redirect_chk>
                <cms:editable name='old_pagename' type='text' label='Old pagename' desc='must be in format of page-name-example' validator='regex=/^[a-z0-9-]+$/' required='1' order='1' not_active=redirect_chk />
                <cms:editable name='new_pagename' type='text' label='New pagename' desc='must be in format of page-name-example' validator='regex=/^[a-z0-9-]+$/' required='1' order='2' not_active=redirect_chk />
                <cms:editable type="dropdown" name="redirect_type" label='Type of redirect?' opt_values="Select=- | Permanent=perm | Temporary=temp" required='1' order='3' not_active=redirect_chk />
            </cms:repeatable>
    </cms:globals>
</cms:template>


Code for handing routing:
Code: Select all

<cms:if k_matched_route='page_view' >
    <cms:pages masterpage='your-template-filename.php' page_name=rt_page_name >
        <cms:no_results>
            <cms:show_globals>
                <cms:if enable_redirects eq 'yes'>
                    <cms:show_repeatable 'template_redirects'>
                        <cms:if old_pagename=rt_page_name>
                            <!-- redirect if matches -->
                            <cms:redirect "<cms:show k_template_link /><cms:show new_pagename />.html" permanently="<cms:if redirect_type='perm'>1<cms:else />0</cms:if>" />
                        </cms:if>
                    </cms:show_repeatable>
                    <!-- show page not found if no match -->
                    <cms:embed 'page_not_found.html' />
                <cms:else />
                    <!-- show page not found if redirects disabled -->
                    <cms:embed 'page_not_found.html' />
                </cms:if>
            </cms:show_globals>
        </cms:no_results>
        <!-- found page show content -->
    </cms:pages>
</cms:if>

<cms:if k_matched_route='list_view'>
    <!-- list view contents -->
</cms:if>
Yes, this should work. It is a rare gem to read a solution posted by OP :)

I would've moved that to filter, though. It is a personal preference, maybe.
I just have no experience with route filters so did not take that approach with this solution but I will look into it further to see what it does. Thanks!
I think we can also use a custom 404.php template to do this job of redirection.

As you know, when Couch does not find a requested page it looks for a template named '404.php' and executes it instead.
This 404.php can be a Couch-managed template so we can place a list of redirections within it (perhaps as a repeatable-region). The template then can check if the current page (i.e. the missing one) matches some rule and redirect there.

Your thoughts, gentlemen?
Routed templates lose ability to display page-view, when clicked magnifying-glass link "View" from Admin Panel. A code placed in template's page-view redirects visitor from template.php?p=123 to correct routed link.
KK wrote: I think we can also use a custom 404.php template to do this job of redirection.

As you know, when Couch does not find a requested page it looks for a template named '404.php' and executes it instead.
This 404.php can be a Couch-managed template so we can place a list of redirections within it (perhaps as a repeatable-region). The template then can check if the current page (i.e. the missing one) matches some rule and redirect there.

Your thoughts, gentlemen?


I had this in mind when posted about a message displayed to the visitor. My concern about this method is for search engines that will receive // header('HTTP/1.1 404 Not Found'); // before the redirect from 404.php happens. If Google can glue all those links without issues, then a repeatable region in 404 is probably the best variant, if one can pass missing page-name/id+template_id to the 404.php to correctly find the match.
12 posts Page 1 of 2