Jump to content

How to create a redirect module


Recommended Posts

Hey dear PrestaShop developer Comunity..
so i have been tearing my hair out over a minor thing that i just can't seem to find any valuable information on.
Neiter in the dev.docs nor through google ( maby i do not know what I am searching for).

----------------------------------------------------------------------------------------

So my goal is to write a module that does redirects in Prestashop 1.7.7.1 

I have URL 1 and I want the module to redirect to URL 2

For example:
myshop.com/specialBlogPage -> ( redirects to) -> myshop.com/blog/43-yes-verry-special

myshop.com/amazingbracelet -> (redirects to) -> myshop.com/brachelets/23-saphires-and-ruby-bracelet

I wanna make it modular in the backend ( manage redirects i made)

----------------------------------------------------------------------------------------

If someone could help me  / point me to a usefull resource on how to do redirects in Prestashop 1.17.7.1 I would be forever thankfull ❤️ 

 

Edited by Sebastan12
Better clarification of the question (see edit history)
Link to comment
Share on other sites

  • Sebastan12 changed the title to How to create a redirect module

Hmm, How are you creating these myshop.com/specialBlogPage  urls ?

if they are CMS pages, IIRC there should be some built-in feature in the Prestashop to redirect them to your custom URL.

 

But instead, if you are creating them using your module controller,  like the content of the page might change depend on the parameters value,

For example controller is "Blog",  and parameter is "Page" and value is "special" then it will open your special blog page.

Then you can add routes using "moduleRoutes" in your module for them.

First, Make a module or go to this link to generate a simple one : https://validator.prestashop.com/generator

Second, Add this Hook to the install section :

$this->registerHook('moduleRoutes')

Third, Add this section to your module code in same file (YourModuleName.php) as the install section above :

public function hookModuleRoutes($params){
      $my_routes = array(
		'YourRouteName1' => array(
              'controller' => 'NameOfController1',
              'rule' => 'blog/{id}-{name}/',
              'keywords' => array(
                  'id' => array('regexp' => '[\w]+', 'param' => 'id'),
                  'name' => array('regexp' => '[\w]+', 'param' => 'name'),
               ),
               'params' => array(
                   'fc' => 'module',
                   'module' => 'YourModuleName'
               )
           ),
		'YourRouteName2' => array(
              'controller' => 'NameOfController2',
              'rule' => 'branchelets/{id}-{name}',
              'keywords' => array(
                  'id' => array('regexp' => '[\w]+', 'param' => 'id'),
                  'name' => array('regexp' => '[\w]+', 'param' => 'name'),
               ),
               'params' => array(
                   'fc' => 'module',
                   'module' => 'YourModuleName'
               )
           )
		);
		
        return $my_routes;
    }

 

This will make it that instead of

MyShop.com/Modules/YourModuleName/?Controller=NameOfController1&id=43&name=Yes-very-special  (If friendly URL turned on)

Your page will show up as  myshop.com/blog/43-yes-verry-special

Same goes for the 2nd URL.

MyShop.com/Modules/YourModuleName/NameOfController2/id=23&name=saphires-and-ruby-bracelet  --> myshop.com/brachelets/23-saphires-and-ruby-bracelet

Edited by Pedram (see edit history)
  • Like 1
Link to comment
Share on other sites

  • 4 weeks later...
  • 10 months later...
  • 2 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...