Post by developer123123 » Wed Apr 12, 2023 3:40 am

Currently using open cart 3.0.2.0, php 7.4.

when I type a space in the search bar ("test "), the result url will contain %20(test%20) which leads to a wrong page. How do I replace the %20(space char) from the url when executing the search function?

I have tried to use str_replace in the source file : public_html/storage/modification/catalog/controller/product/search.php

something like

Code: Select all

         $url = str_replace('', '%20', $url);
and also some attempt in the .htaccess folder(alter the rewrite rules). but none of those works.

Any one have any idea on this? ???

New member

Posts

Joined
Tue Oct 26, 2021 3:36 am

Post by thekrotek » Wed Apr 12, 2023 4:00 am

With your code you're replacing empty string with %20. Swap the arguments.

Professional OpenCart extensions, support and custom work.
Contact me via email or Skype by support@thekrotek.com


User avatar
Expert Member

Posts

Joined
Sun Jul 03, 2016 12:24 am


Post by developer123123 » Wed Apr 12, 2023 4:10 am

I tried to swap argument, but didnt do anything, I think the .htaccess file might do it :

I have tried the folloing code in .htaccess

Code: Select all

RewriteEngine On
RewriteBase /

# external redirect from actual URL to pretty one (remove query string)
RewriteCond %{THE_REQUEST} \s/+content\.php\?page=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L,NE]

# convert all space (%20) to hyphen
RewriteRule "^(\S*) +(\S* .*)$" $1-$2 [N,NE]
RewriteRule "^(\S*) (\S*)$" $1-$2 [L,R=302,NE]

# rewrite rule to call actual PHP handler
RewriteRule ^([^./]+)\.html$ content.php?page=$1 [L,QSA,NC]
it helps when there's only 1 extra space, like "test ". and it changes to "test-"

but it wont work if there's more then 1 space. eg "test " => "test%20%20"

If any one knows the trick on .htaccess it would be helpful!

New member

Posts

Joined
Tue Oct 26, 2021 3:36 am

Post by Cue4cheap » Wed Apr 12, 2023 11:23 am

developer123123 wrote:
Wed Apr 12, 2023 3:40 am

I have tried to use str_replace in the source file : public_html/storage/modification/catalog/controller/product/search.php
BTW that isn't the source file but the modified file so if you did a refresh of modifications it'll wipe out whatever you programmed into that file.

Mike

cue4cheap not cheap quality


Expert Member

Posts

Joined
Fri Sep 20, 2013 4:45 am

Post by paulfeakins » Wed Apr 12, 2023 7:21 pm

developer123123 wrote:
Wed Apr 12, 2023 3:40 am
I have tried to use str_replace in the source file : public_html/storage/modification/catalog/controller/product/search.php

something like

Code: Select all

         $url = str_replace('', '%20', $url);
You shouldn't be modifying stuff in "public_html/storage/modification" it will just get deleted and your work will be lost.

Also a fresh OpenCart doesn't have this issue so it's something you've done - look at removing some dodgy code rather than adding more.

UK OpenCart Hosting | OpenCart Audits | OpenCart Support - please email info@antropy.co.uk


User avatar
Legendary Member
Online

Posts

Joined
Mon Aug 22, 2011 11:01 pm
Location - London Gatwick, United Kingdom

Post by by mona » Wed Apr 12, 2023 10:45 pm

As Mike and Paul have both said, you do not edit your modification file as when you refresh cache it will be gone.

You also do not replace spaces in search terms in your htaccess, you trim the term in your controller before anything else.
find

Code: Select all

if (isset($this->request->get['search'])) {
$search = $this->request->get['search'];
} else {
$search = '';
}
change to

Code: Select all

if (isset($this->request->get['search'])) {
// reduce consecutive spaces and trim
$search = trim(preg_replace('/\s+/', ' ', $this->request->get['search']));
} else {
$search = '';
}

DISCLAIMER:
You should not modify core files .. if you would like to donate a cup of coffee I will write it in a modification for you.


https://www.youtube.com/watch?v=zXIxDoCRc84


User avatar
Expert Member

Posts

Joined
Mon Jun 10, 2019 9:31 am
Who is online

Users browsing this forum: No registered users and 15 guests