I'm writing a custom Magento module to handle 404 URLs of the web site to fulfill some marketing requirements. I'm wondering what would be the best way to capture the 404s and pass those through my module before it get to the Apache 404 page?
I have scene few ways of people doing it, but not specific to Magento though. So please feel free to provide your expert advice. Thank you!
Found another easy way of doing it, Go to Admin > System > Configuration > Web > Default Pages
, then change the value for "Default No-route URL" to whatever your modules controller action e.g myweb ew404manager\index
This way when a 404 is occur it will point to the controller action defined under this field.
You don't need a method specific to your CMS. Just add to your .htaccess file:
ErrorDocument 404 /some_php_script.php
Or this, with mod_rewrite:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ some_php_script.php/$1
The latter has the advantage of letting you access POST data. This php script is in charge to connect to your CMS code somehow.
You should be able to get the original url with $_SERVER['REQUEST_URI']
. This script is in charge of replying the status, with either 200 OK
or 404 Not Found
, using the header() function
Edit: Turns out that magento already does this (all the non-static urls are routed to index.php). I'm not familiar enough with the CMS, but it seems it displays a page with identifier "no-route", so hopefully you can replace the contents of this page with something you need (the relevant code is the Mage_Cms_Model_Page
class, NOROUTE_PAGE_ID
, and the noRoutePage()
function). grep -r
is your friend when trying to understand how to deal with code other people wrote :D
You can accomplish this by rewrite the cms controller for noRoute
Take a look @
Overriding magento cms controller for noRoute action