Page 1 of 1

How to rewrite index.php?route to seo friendly urls?

Posted: Tue Oct 14, 2014 10:02 pm
by chongshengdz
How to rewrite index.php?route to seo friendly urls for opencart 2.0?
for example:
from index.php?route=product/search&search=keyword to search&search=keyword

Re: How to rewrite index.php?route to seo friendly urls?

Posted: Thu Oct 16, 2014 9:53 pm
by MarketInSG
you can consider this: http://www.opencart.com/index.php?route ... n_id=12422

It's ready for 2.0 use, but we have not publish the update.

Re: How to rewrite index.php?route to seo friendly urls?

Posted: Sun Oct 19, 2014 9:53 pm
by chongshengdz
Do you have a working demo site for it?
It seems your demo site does not work.

Re: How to rewrite index.php?route to seo friendly urls?

Posted: Thu Oct 30, 2014 1:22 am
by surfdog
when will this extension be made available for version 2.0? thanks

Re: How to rewrite index.php?route to seo friendly urls?

Posted: Thu Oct 30, 2014 1:23 am
by surfdog
MarketInSG wrote:you can consider this: http://www.opencart.com/index.php?route ... n_id=12422

It's ready for 2.0 use, but we have not publish the update.
when are you going to be publishing this?

Thanks

Re: How to rewrite index.php?route to seo friendly urls?

Posted: Sat Nov 01, 2014 4:57 pm
by MarketInSG
you can drop us a support ticket for you to send the pre released files. We're occupied with updating the last few mods and haven't got time to push the updated mods

Re: How to rewrite index.php?route to seo friendly urls?

Posted: Sat Nov 01, 2014 8:00 pm
by virgotch
you can try to edit this code for 2.0 , this code worked for 1.5
Go to /system/library/url.php and look at the public function link. Replace the function with this version:

Code: Select all

public function link($route, $args = '', $connection = 'NONSSL') {
    if ('NONSSL' == $connection) {
        $url = $this->url;
    } else {
        $url = $this->ssl;  
    }

    if ('common/home' == $route) {
        if ($args) {
            $url .= '?' . str_replace('&', '&', '&' . ltrim($args, '&')); 
        }
    } else {
        $url .= 'index.php?route=' . $route;
        if ($args) {
            $url .= str_replace('&', '&', '&' . ltrim($args, '&')); 
        }
    }

    foreach ($this->rewrite as $rewrite) {
        $url = $rewrite->rewrite($url);
    }

    return $url;
}
run some tests and if the SEO URLs are enabled, it becomes necessary to make one single edit in the /catalog/controller/common/seo_url.php to avoid an "Undefined index" error.

nside public function rewrite, replace this line:

Code: Select all

parse_str($url_info['query'], $data);
With this one:

Code: Select all

if (isset($url_info['query'])) parse_str($url_info['query'], $data);

Re: How to rewrite index.php?route to seo friendly urls?

Posted: Fri Feb 28, 2020 12:06 am
by bonyta2442
Thank you for the solution.
I have OC 2.0.1.1 tested the above code:
i have only edit in the /catalog/controller/common/seo_url.php
replacing with:

Code: Select all

if (isset($url_info['query'])) parse_str($url_info['query'], $data);
And the page loads:
https://www.dbkdimitrov.com/search&search=audi

but can't add the rewrite rule like the bottom:

Code: Select all

<file path="catalog/controller/common/seo_url.php">
		<operation>
			<search>
				<![CDATA[
					class ControllerCommonSeoUrl extends Controller {
				]]>
			</search>
			<add position="after">
				<![CDATA[
					private $url_list = array (
						'common/home' => '',
						'account/wishlist' => 'whishlist',
						'account/account' => 'account',
						'account/edit' => 'account/edit',
						'account/password' => 'account/password',
						'account/address' => 'account/address',
						'account/address/edit' => 'account/address/edit',
						'account/reward' => 'account/reward',
						'account/login' => 'account/login',
						'account/logout' => 'account/logout',
						'account/order' => 'account/order',
						'account/order/info' => 'account/order/info',
						'account/newsletter' => 'account/newsletter',
						'account/forgotten' => 'account/forgot',
						'account/download' => 'account/download',
						'account/return' => 'account/return',
						'account/return/add' => 'account/return/add',
						'account/transaction' => 'account/transaction',
						'account/register' => 'account/register',
						'account/recurring' => 'account/recurring',
						'account/voucher' => 'account/voucher',
						'affiliate/account' => 'affiliate',
						'affiliate/edit' => 'affiliate/account',
						'affiliate/password' => 'affiliate/password',
						'affiliate/payment' => 'affiliate/payment',
						'affiliate/tracking' => 'affiliate/tracking',
						'affiliate/transaction' => 'affiliate/transaction',
						'affiliate/logout' => 'affiliate/logout',
						'affiliate/forgotten' => 'affiliate/forgotten',
						'affiliate/register' => 'affiliate/register',
						'affiliate/login' => 'affiliate/login',
						'checkout/cart' => 'cart',
						'checkout/checkout' => 'checkout',
						'checkout/voucher' => 'voucher',
						'checkout/success' => 'success',
						'information/contact' => 'contact',
						'information/ofer' => 'ofer',
						'information/sitemap' => 'sitemap',
						'product/special' => 'special',
						'product/manufacturer' => 'manufacturer',
						'product/compare' => 'compare',
						'product/search' => 'search',
						'product/search&search' => 'search&search', // this doesn't work! please help!
                        
                        
					);
				]]>
			</add>
		</operation>
		<operation>
			<search>
				<![CDATA[
					$this->request->get['route'] = 'error/not_found';
				]]>
			</search>
			<add position="after" offset="4">
				<![CDATA[
					if ( $_s = $this->setURL($this->request->get['_route_']) ) {
						$this->request->get['route'] = $_s;
					}
				]]>
			</add>
		</operation> 
		<operation>
			<search>
				<![CDATA[
					if ($url) {
				]]>
			</search>
			<add position="before" offset="3">
				<![CDATA[
					if( $_u = $this->getURL($data['route']) ){
                        $url .= $_u;
                        unset($data[$key]);
                    }
				]]>
			</add>
		</operation>
		<operation>
			<search>
				<![CDATA[
					return $link;
				]]>
			</search>
			<add position="after" offset="2">
				<![CDATA[
					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;
					}
				]]>
			</add>
		</operation>
	</file>