【问题标题】:PHP preg_match regex redirect urlPHP preg_match 正则表达式重定向网址
【发布时间】:2019-09-24 13:26:18
【问题描述】:

我正在开发一个 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/*

我需要将这些链接模式重定向到主页:http://mywebsite.com/

我有一个中间件,我试图将所有链接放在一个数组中,我循环遍历数组,同时采用.com/ 之后的路径并使用substr 来测试它是否以request()->path() 开头。但我认为应该使用正则表达式 preg_match 来完成,但我不知道该怎么做。

提前致谢。

【问题讨论】:

标签: php regex laravel


【解决方案1】:
$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.
}

【讨论】:

  • 我不想提mywebsite,我只能访问request()->path(),它返回.com/之后的部分,我也喜欢我提到的,我需要重定向content/* 表示应重定向content/test 之类的路径。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-05
  • 2014-06-28
  • 1970-01-01
相关资源
最近更新 更多