URL rewriting for Magento module

对着背影说爱祢 提交于 2019-12-21 02:46:07

问题


I have created a New Module in Magento and named it as "article". It has two Front end controllers index and article.

And in the article controller i have an action called "archives" to list the articles on the front end / website based on the archives. Everything is fine except the URL.

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

What i actually need is http://mydemostore/article/archives/01/2011

I don't want to have an extra "article" (the controller name)

I know since my module name and controller name are same i am getting this. If i would have placed my actions inside the indexcontroller, i would have got this done. But it was not working.

So what i need right now is I don't want to move my actions from "article" to "index" controllers rather i just want to change

from

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

to

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

using Zend routing or basic PHP URL rewriting on .htaccess file.

Kindly help me how to get this done by using either of these two ways or both the ways.

Thanks a lot in advance for checking with my question !!!


回答1:


I've not done this myself, only read about it here, but here goes...

In you 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>

The node <article_article_archives> isn't strictly formed, it only needs to be unique from other rewrites.




回答2:


Something like this in .htaccess

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

May remove duplicates, but I wouldn't opt for it

A more general case for duplicates:

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



回答3:


Well, you might not be open to this option if you have several controllers in your module, but it is a valid solution without creating extra rewrite rules in your .htaccess, etc. It is possible to set your "articles" module up with an IndexController.php instead of an ArticleController.php. Then you could access the pages with [http://]mydemostore/article/archives/01/2011.... where archivesAction() is a method in the IndexController.php. Magento automatically maps the IndexController.php to /yourmod/index/ or just simply /yourmod/.




回答4:


Well, you might not be open to this option if you have several controllers in your module, but it is a valid solution without creating extra rewrite rules in your .htaccess, etc. It is possible to set your "articles" module up with an IndexController.php instead of an ArticleController.php. Then you could access the pages with [http://]mydemostore/article/archives/01/2011.... where archivesAction() is a method in the IndexController.php. Magento automatically maps the IndexController.php to /yourmod/index/ or just simply /yourmod/.


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

However, the following urls will refer to the controller-action pair mentioned:

  • mydemostore/article/index/archives/
  • mydemostore/article//archives/

Regarding the original question, saving programmatic URL rewrites within Magento would be the best practice, but not necessarily the most practical or quickest to implement; of course, Apache directives (e.g. .htaccess or site conf files) will be the quickest but not most observant of best practices.



来源:https://stackoverflow.com/questions/4768681/url-rewriting-for-magento-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!