O
O
Ostap262015-10-21 21:19:28
Magento
Ostap26, 2015-10-21 21:19:28

How to connect the Magento module I created so that it reacts to events?

I created a module in Magento, connected it in the admin panel, but I can’t use it on the site in any way. When going to the main page of the site/index.php should make a 301 redirect to the main page without index.php.
Here is the module files code:
app/local/Mymodule/SEO/etc/config.xml

<?xml version="1.0" ?>
<config>
<global>
    <modules>
        <Mymodule_SEO>
            <version>0.0.1</version>
        </Mymodule_SEO>
    </modules>
          <models>
            <SEO>
                <class>Mymodule_SEO_Model</class>
            </SEO>
        </models>
    <events>
      <controller_action_layout_generate_blocks_before>
        <observers>
          <SEO>
            <type>singleton</type>
            <class>SEO/Observer</class>
            <method>redirect</method>
          </SEO>
        </observers>
      </controller_action_layout_generate_blocks_before>
    </events>
</global>
</config>

app/local/Mymodule/SEO/model/Observer.php:
class Mymodule_SEO_Model_Observer
{
  public function redirect($observer) {
      if($_SERVER['REQUEST_URI'] == "/index.php") {
        header("HTTP/1.1 301 Moved Permanently");
        header("Location: ".Mage::getBaseUrl('web'));
        exit();
      }
}

app/etc/modules/Mymodule_SEO.xml
<?xml version="1.0"?>
<config>
    <modules>
        <Mymodule_SEO>
            <active>true</active>
            <codePool>local</codePool>
        </Mymodule_SEO>
    </modules>
</config>

Tried different events. Nothing happens when I go to sitedomen.lc/idex.php. I checked in the admin panel the module is enabled. I can also call redirect() in the test.php file created in the root of the site:
<?php
require_once 'app/Mage.php';
Mage::app();

$Net=Mage::getModel("SEO/observer");
$Net->redirect();

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Oleg Batishchev, 2015-11-03
@z0rg

Your task is solved through .htaccess if you have Apache or through location{} in the config if you have Nginx + enabled rewrite in Magento.

  1. The test with test.php is not correct at all.
  2. You have used the controller_action_layout_generate_blocks_before event. Think now by the name how often it will be called, very often, just horror how often, and you just do a redirect.
  3. app/local/Mymodule/SEO/model/Observer.php - Model must be capitalized

A
Antiless, 2015-11-17
@Antiless

if you don't need "index.php" in the url, then you can disable it in System/Configuration/General/Web/Search Engines Optimization/Use Web Server Rewrites: Yes
or you can go to Catalog/URL Rewrite Management / add url rewrite / Create URL Rewrite: custom / Redirect: Permanentl (301)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question