[英]PHP preg_match regex redirect url
I working on a Laravel app and I have these links:我正在开发一个 Laravel 应用程序,我有以下链接:
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/我需要将这些链接模式重定向到主页: 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()
.我有一个中间件,我试图将所有链接放在一个数组中,我循环遍历数组,同时采用.com/
之后的路径并使用substr
来测试它是否开始使用request()->path()
。 But I think it should be done using regex preg_match
but I have no clue how to do this.但我认为应该使用正则表达式preg_match
来完成,但我不知道如何做到这一点。
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.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.