Raghwendra Ojha
Raghwendra Ojha

Reputation: 11

How can i stop / redirect non seo friendly url to their url alias in OpenCart 2.x?

How can i redirect non seo friendly url to url alias permanently(301) in OpenCart 2.x ? For example, For me both url is working http://example.com/index.php?route=account/login and http://example.com/login

I would like to redirect 301 when some one hit url example{dot}com/index.php?route=account/login to their respective url alias url example{dot}com/login.

Upvotes: 0

Views: 1040

Answers (1)

zed Blackbeard
zed Blackbeard

Reputation: 760

I think Following vqmod_seo_url.xml will solve your problem

<modification>
    <id>seo_url_core</id>
    <version>1.4.x and above</version>
    <vqmver required="true">2.x.x</vqmver>

    <file name="catalog/controller/common/seo_url.php">
        <operation error="skip">
            <search position="before" offset="0">
        <![CDATA[
        public function index() {
        ]]>
            </search>
            <add>
        <![CDATA[
                /* SEO Custom URL */
                private $url_list = array (
                    'common/home'       => '',
                    'checkout/cart'     => 'cart',
                    'account/register'  => 'register',
                    'account/wishlist'  => 'wishlist',
                    'checkout/checkout' => 'checkout',
                    'account/login'     => 'login',
                    'product/special'   => 'special',
                    'affiliate/account' => 'affiliate',
                    'checkout/voucher'  => 'voucher',
                    'product/manufacturer' => 'brand',
                    'account/newsletter'   => 'newsletter',
                    'account/order'        => 'order',
                    'account/account'      => 'account',
                    'information/contact'  => 'contact',
                    'account/return/insert' => 'return',
                    'information/sitemap'   => 'sitemap',
                    );
                /* SEO Custom URL */
        ]]>
            </add>
        </operation>
        <operation error="skip">
            <search position="before">
        <![CDATA[
        if (!isset($this->request->get['route'])) {
        ]]>
            </search>
            <add>
        <![CDATA[
        /* SEO Custom URL */
                    if ( $_s = $this->setURL($this->request->get['_route_']) ) {
                            $this->request->get['route'] = $_s;
                }/* SEO Custom URL */
        ]]>
            </add>
        </operation>
        <operation error="skip">
            <search position="before" offset="3">
        <![CDATA[
        if ($url) {
        ]]>
            </search>
            <add>
        <![CDATA[
         /* SEO Custom URL */
                  if( $_u = $this->getURL($data['route']) ){
                    $url .= $_u;
                    unset($data[$key]);
                  }
                 /* SEO Custom URL */ 
        ]]>
            </add>
        </operation>
        <operation error="skip">
            <search position="after" index="1" offset="2">
        <![CDATA[
        return $link;
        ]]>
            </search>
            <add>
        <![CDATA[
         /* SEO Custom URL */
                    public function getURL($route) {
                            if( count($this->url_list) > 0) {
                                 foreach ($this->url_list as $key => $value) {
                                    if($route == $key) {
                                        return '/'.$value;
                                    }
                                 }
                            }
                            return false;
                    }
                    public function setURL($_route) {
                            if( count($this->url_list) > 0 ){
                                 foreach ($this->url_list as $key => $value) {
                                    if($_route == $value) {
                                        return $key;
                                    }
                                 }
                            }
                            return false;
                    }/* SEO Custom URL */ 
        ]]>
            </add>
        </operation>
    </file>

</modification>

Upvotes: 0

Related Questions