Shallow URLs? (no hierarchy)
Does anyone know how I can make the canonical URL of all my C5 pages shallow so that they appear to be directly under the root domain? (rather than grouped in hierarchical subfolders)
I want this: mysite.com/baseball-cards/
rather than this: mysite.com/baseball-products/baseball-cards/
(Imagine I have 30 products, I don't want them all located in the site root in my sitemap with my other files/products, but neatly in a "baseball products" folder - however I don't want them to indicate their position in the URL.)
I know I can create a redirect for this, but I want the shorter URL to be the canonical URL, not just a redirect to the long one.
Why do I want to do this?
1. I am convinced there is some sort of SEO value to this - something to do with the page being closer to the root domain than filed down lower in the site structure.
2. If I want to move the page around my site navigation-wise, or change "subfolder" names, my canonical URLs do not need to change (remain persistant) - v. good for maintaining links.
Has anyone done this before with c5 that can help me? I've done it with CMSMS & Wordpress in the past with htaccess/mod_rewrite - just didn't know if c5 calculates URLs in a tricky way...
as for actually doing it
you can add an alternative url on each of the pages, but These do not filter down,
ie /root/categories/products could be accessed by /root/products
but /root/categories/products/options would not inherit this to produce /root/products/options
If you are willing to go that root, you could use htaccess to the forward all the categories to go to products, I do it quite a bit but nothing as big as a products db so its normally only one or two pages.
Im using the following to create canonical links but this would need to be tweaked to use the new urls
<?php $canonical = $c->getCollectionPointerID() ? Page::getByID($c->getCollectionPointerID()) : $c; $nh = Loader::helper('navigation'); printf('<link rel="canonical" href="%s" />', $nh->getCollectionURL($canonical));echo "\n"; ?>
I am going to assume there is a way to use $_SERVER['REQUEST_URI'] with some regular expression instead?, to maybe set the canonical links to the shortened url when on the categories/products pages.
Im not sure about this but maybe something like(wrapped with an if for only cat products)
$canonical = preg_replace('/([a-zA-Z-]+)\/([a-zA-Z-]+)(\/)?/i', '$2', $_SERVER['REQUEST_URI']);
again not to sure if this would work but you may be able to get the page object using Page::getByPath("/products"); if you prefer to work with the object? - (Thinking about this it should be possible grab the page object and then set the extra URL for the page, I dont know the function used? I'm afraid)
I think a challenge may also arise when you use the page selector which will use the default page url instead of your nice canonical link.
and some on the forum
Now I dont know if this will work its all theory
<?php $c = Page::getByPath($_SERVER['_REQUEST_URL']); $ncID = $c->addCollectionAlias($c); ?>
Anyway - I've added it as a feature request here:
Maybe it'll be added to the core one day... :-)