简体   繁体   中英

PHP preg_match regex redirect url

I working on a Laravel app and I have these links:

http://mywebsite.com/content/*
http://mywebsite.com/products/*
http://mywebsite.com/search/node*
http://mywebsite.com/user/*
http://mywebsite.com/escal/list/*
http://mywebsite.com/node/*
http://mywebsite.com/download/file/fid/*

I need to redirect these link patterns to the home page: http://mywebsite.com/

I have a middleware where I tried to put all the links in an array, I loop through the array while taking the path after .com/ and using substr to test if it startsWith request()->path() . But I think it should be done using regex preg_match but I have no clue how to do this.

Thanks in advance.

$paths = [
'content',
'products',
'search\/node',
'user',
'escal\/list',
'node',
'download\/file\/fid',
];

$patternWithPaths = sprintf('/^http:\/\/mywebsite\.com\/(%s)\//', implode('|', $paths));

if(preg_match($patternWithPaths))
{
  // A match was found, do redirect.
}

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