Rewriting URLs for Magento Module

I created a new module in Magento and called it an “article”. It has two indexes and front controller articles.

And in the article controller, I have an action called “archives” to list articles on the front side / website based on archives. Everything is fine except the URL.

The working URL right now: [http: //] mydemostore / article / article / archives / 01/2011

I really need http: // mydemostore / article / archives / 01/2011

I do not want to have an additional "article" (controller name)

I know, since my module name and controller name are the same, I get this. If I put my actions in an indexcontroller, I would do that. But he did not work.

So, what I need right now, I do not want to move my actions from the "article" to the "index" controllers, but just want to change

of

[http: //] mydemostore / article / article / archives / 01/2011 to

to

[http: //] mydemostore / article / archives / 01/2011

using Zend routing or basic rewriting of PHP code in a .htaccess file.

Please help me how to do this using either of these two methods or both.

Thanks so much for checking my question !!!

+4
source share
4 answers

I myself did not do this, I only read about it here , but here it goes ...

In the config.xml file:

<config> <global> <rewrite> <article_article_archives> <from><![CDATA[#^/article/archives/#]]></from> <to><![CDATA[/article/article/archives/]]></to> </article_article_archives> </rewrite> </global> </config> 

node <article_article_archives> not strict, it should be unique only from other rewritable files.

+3
source

Something like this in .htaccess

 RewriteEngine On RewriteRule ^(article/)* article/ [L] 

You can remove duplicates, but I would not select it

More general case for duplicates:

 RewriteEngine On RewriteRule ^([^/]+/)* $1 [L] 
0
source

Well, you may not open this option if there are several controllers in your module, but this is a valid solution without creating additional rewrite rules in your .htaccess, etc. You can install the "articles" module with IndexController.php instead of ArticleController.php. You can then access the pages using [http: //] mydemostore / article / archives / 01/2011 .... where archivesAction () is the method in IndexController.php. Magento automatically displays IndexController.php in / yourmod / index / or just / yourmod /.

0
source

Well, you may not have to open this option if you have several controllers in your module, but this is a valid solution without creating additional rewrite rules in your .htaccess, etc. It is possible to install the "articles" module using IndexController.php of the articleController.php. You can then access the pages using [http: //] mydemostore / article / archives / 01/2011 .... where archivesAction () is the method in IndexController.php. Magento automatically displays IndexController.php in / yourmod / index / or simply just / yourmod /.


Unfortunately, mydemostore/article/archives/01/2011 IndexController->archivesAction will NOT refer to IndexController->archivesAction , but archivesController->01Action , which will lead to an error, since a method / function in PHP can only start with an underscore or letter.

However, the following URLs will refer to the controller pair mentioned:

  • mydemostore / articles / index / archives /
  • mydemostore / article // Archives /

Regarding the original question, keeping software URLs for rewriting in Magento would be best practice, but not necessarily the most practical or fastest to implement; Of course, Apache directives (such as .htaccess or site conf files) will be the fastest, but not the best practices.

0
source

Source: https://habr.com/ru/post/1336596/


All Articles