Step 1. Create phtml file for the redirect handling
First of all create a phtml file into the app/design/frontend/base/default/template/page/ or app/design/frontend/default/your_custom_theme/template/page/ directory of your active theme. Call it something like redirect-auth.phtml and copy and paste the below code into it:
<?php //echo "Authorize Page"; Mage::getSingleton('customer/session')->setBeforeAuthUrl($this->getRequest()->getRequestUri()); if(!Mage::getSingleton('customer/session')->isLoggedIn()) { header("Status: 301"); header('Location: '.$this->getUrl('')); exit; } ?>
From the above code the 301 redirects is on the home page you can redirect the user on the login page or somewhere else as well just by changing the header location code Suppose you want to redirect on the Login page replace this header(‘Location: ‘.$this->getUrl(”)); by the code header(‘Location: ‘.$this->getUrl(‘customer/account/login’)); .
Step 2. Editing of the page.xml
For the redirection available to all of the page templates on our website we need to modify the page.xml. For this navigate to the app/design/frontend/base/default/layout/ or app/design/frontend/default/your_custom_theme/layout/page.xml in your active theme, add the following line of code:
below the code
<block type="page/html" name="root" output="toHtml" template="page/3columns.phtml">
Now the final code will be :
<default translate="label" module="page"> <label>All Pages</label> <block type="page/html" name="root" output="toHtml" template="page/3columns.phtml"> <block type="page/html" name="redirect-auth" as="auth-redirect" template="page/redirect-auth.phtml"/> ....................... ....................... .......................
Step 3. Now Editing of the page layout templates:
Now we need to include the below code for all of the page templates i.e.
1column.phtml
2columns-left.phtml
2columns-right.phtml
3columns.phtml
and others
Insert the following line of the code on the top of the file into all of the available templates
getChildHtml(‘redirect-auth’); ?>
which ensures that the redirect is included before any of the page HTML renders. You have almost done but there may be some pages which don’t need to be protected just like as the contact form login and register page, for making these page public we need some exception code which you have to insert into the appropriate xml files.
Let me explain it here with one of the example suppose you want to public the contact form then we must have to make the changes into the contacts.xml file for this navigate to the app/design/frontend/base/default/layout/ or app/design/frontend/default/your_custom_theme/layout/contacts.xml and add the below code
into it after the
<reference name="root"> <remove name="redirect-auth" /> <action method="setTemplate"><template>page/2columns-right.phtml</template></action> <action method="setHeaderTitle" translate="title" module="contacts"><title>Contact Us</title></action> </reference>
Thats it and now your user will be able to see the contact form.
Hope it helps, Thanks for reading and enjoy the Magento custom coding. 😉