Magento URL Rewrites – custom url redirect for CMS page

OK, so why this post? Well there just wasn’t very much (or indeed anything at all) on this in Google when I looked. There is loads on how to use Magento’s built in url rewrite system from teh backend. And there’s loads on Stackoverflow and Alan Storm’s blog on how to create a custom controller to over ride the PageController and IndexController. Nothing on how to just create a custom redirect for a cms page though.

I needed to do this because I am doing a project with Peter Warren at Synoptive (Magento product configurators) on a Magento multistore. It is for United Carports (custom built carports), I’ve made them a Magento multistore based on different regions of the USA. So Mid West Store, North East Store, South West store etc. As they have a configurator this means that the cms age which normally reverse proxy’s there needs to be able to have it’s own custom url which worked with the Synoptive configurator.

So the cms page in question had an id of custom-design. I set Magento up so the store codes were included in the url, this meant that the cms page urls were like /mw/custom-design/, /nw/custom-design/ and /se/custom-design/ etc

The Synoptive configurator needed them passed in like /mw/custom-design/#/united_carports/mw and nw/custom-design/#/united_carports/nw etc

So how do you do that then? Well creating a load of differnet store specific versions of the cms page didn’t work. And the Magento way didn’t work (here’s a section pulled from the email I sent Peter on that);

——————————————————————————–

On the Catalog ->URL Rewrite Management, it’s just not supposed to work like that. It is a complicated system and I don’t pretend to be an expert but trying to rewrite for static files or fictitious urls isn’t what it’s there for, they are all run through index.php which is looking for the MVC module/controller/action url structure there’s a whopper of an article on it here,

You can see the relevant section here;

http://www.solvingmagento.com/magento-url-rewrites/

Limitations of URL rewrites

The most commom oversight with URL rewrites is an attempt to use them for static files. This simply will not work. When a request for a static file is processed, the Magento rewrite engine is not even engaged. So it is defined by the rules in the .htaccess file: if a requested file or directory exists on the server – return it directly. Magento URL rewriting requires that the request is first routed to the index.php file, which starts Magento and prodives access to its functionality including URL rewrites. Therefore, creating URL rewrites for static files, i.e. catalog images and other content of the media folder, CSS and JS files, would have no effect.

————————————————————————————-

I had a go at doing a rewrite on Mage_Core_Model_Url but that seemed both fraught with problems, and didn’t want to work anyway.

We got it working using Apache url rewrites. Turn the rewrite engine on, uncomment the RewriteBase and then create your RewriteRule. Remember that setting the [L] flag means that further rules aren’t processed and Magento won’t get involved. The exmaple below is Peter’s credit, he’s used a variable $1 to duplicate the store code portion further along the url

############################################
## enable rewrites

    Options +FollowSymLinks
    RewriteEngine on

############################################
## you can put here your magento root folder
## path relative to web root

RewriteBase /

###### United rewrites ######
RewriteRule ^(.*)/custom-design/proxy/?$ /$1/custom-design/#/united_carports/$1() [L,NE,R=302]