Home > database >  How to redirect to new url if multiple slashes in URI with 'PREG'
How to redirect to new url if multiple slashes in URI with 'PREG'

Time:03-16

I am using str_replace() for removing extra slashes from url i dont know how to redirect the url to new url if find multi slashes in url?

if(str_replace(':/','://', trim(preg_replace('/\/ /', '/', PERMALINK), '/')))
{
  echo 'Yes found multi slashes redirect it to new url';
}
else
{
  echo 'Not found multi slashes';
}

CodePudding user response:

using the plus symbol in regex means the occurrence of one or more of the previous character. So we can add it in a preg_replace to replace the occurrence of one or more / by just one of them

$url = "site.com/edition/new///";

$newUrl = preg_replace('/(/ )/','/',$url);

// now it should be replace with the correct single forward slash echo $newUrl

  • Related