简体   繁体   中英

Magento Shorten the URL of a Module / Rewrite with .htaccess

On Magento the URL structure of a module will be like this:

example.com/[index.php]/:module/:controller/:action/:param1[/:paramN ...]

I created a gallery module. The gallery contains several album and the URL for each album is like this (following Magento URL structure):

example.com/[index.php]/gallery/controller/view/name/:album_name

but I want the url of each album on the gallery will be like this:

example.com/[index.php]/gallery/:album_name

How can I achieve this? I've tried with .htaccess Rewrite Rule but seems like I got it wrong and it's not working. Here is my copy of Magento .htaccess rewrite rule

<IfModule mod_rewrite.c>
    ## enable rewrites
    Options +FollowSymLinks
    RewriteEngine on

    ## workaround for HTTP authorization
    ## in CGI environment
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    ## Rewrite Rule for Gallery Module
    RewriteRule ^/gallery/(.*)?$ /gallery/category/view/name/$1 [L]


    ## always send 404 on missing files in these folders
    RewriteCond %{REQUEST_URI} !^/(media|skin|js)/

    ## never rewrite for existing files, directories and links
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l

    ## rewrite everything else to index.php
    RewriteRule .* index.php [L]    

</IfModule>

You could accomplish this using magento url rewrite in your confix.xml (without the need to use apache rewrite)

<global>
     <rewrite>
       <fancy_url>
             <from><![CDATA[/customModule\/(.*)/]]></from>
             <to><![CDATA[customModule/controller/view/name/$1/]]></to>
             <complete>1</complete>
        </fancy_url>
     <rewrite>
</global>

Read more @ do simple url rewrite in magento using config.xml

See my answer @ Magento Router Url - Need Hyphnated Path Name on how to access :album_name

如果您需要自定义路由以对许多路由通用,则可以轻松实现自定义路由器类(请Mage_Cms_Controller_Router [link] 及其作为观察者[link]的配置 )。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM