简体   繁体   English

PHP和Apache-解析mod_rewrite网址

[英]PHP & Apache - parse mod_rewrite url

I have my server's apache configured to redirect every request into index.php, so that I can use urls like : 我将服务器的apache配置为将每个请求重定向到index.php,以便可以使用以下网址:

http://www.website.com/about/us

Now I need to parse the url in order to determine if each route exists and what to expect from it, so I can set a database table to save the routes and so my clients can edit their routes as they like them to be. 现在,我需要解析url以确定每个路由是否存在以及从中可以得到什么,因此我可以设置一个数据库表来保存路由,以便我的客户可以按自己的意愿编辑其路由。 Thing is, I'm not able to do the parsing so far. 事情是,到目前为止,我还无法解析。

Here's my code : 这是我的代码:

$uri = 'about/us';
$regex = '(page(/<action>))';
if ( ! preg_match($regex, $uri, $matches)) {
    echo 'the route doesn\'t match';
    exit();
}

It always shows up that echo message. 它总是显示该回显消息。 What am I doing wrong? 我究竟做错了什么?

edit : 编辑:

The reason why I need regex is as follows. 我需要正则表达式的原因如下。 Imagine I have a url segment like : 假设我有一个网址段,例如:

/user/yoda

The regex here would be usefull to parse the first argument as an action or page, and the second as a key that will only allow a certain set of chars, like if I want to expect, in this case, the second paramenter only to match "_-azAZ09", something like this. 这里的正则表达式对于将第一个参数解析为一个动作或页面很有用,而第二个参数解析为只允许一组特定字符的键,例如,如果我希望在这种情况下,第二个参数仅匹配“ _-azAZ09”,像这样。 In other words, the set of regex expressions would me compared to the uri in order to determine if any of them matches the uri, and determine the action to take from that on. 换句话说,我将把regex表达式集与uri进行比较,以确定它们中的任何一个是否与uri匹配,并确定从那以后要采取的措施。 Using the explode function would make me evaluate the uri segments later in the code, wich I'd like to avoid. 使用explode函数会使我稍后在代码中评估uri段,但要避免。 If there's no match to the uri, immediatelly forward the page to a 404 one and break the operation. 如果找不到与uri匹配的内容,请立即将页面转发到404页面,然后中断操作。 Don't know if I made myself clear .. 不知道我是否说清楚了..

Instead of using regular expressions, why not simply split the incoming URI? 为什么不简单地拆分传入的URI,而不使用正则表达式?

$uri = 'about/us';
list($page, $action) = explode('/', $uri);

If you need more than two, simply remove the list: 如果需要两个以上,只需删除列表:

$pagePath = explode('/', $uri);

But, if you insist on using regular expressions, this will do: 但是,如果您坚持使用正则表达式,则可以这样做:

$regex = '#(?P<page>.*)/(?P<action>.*)#';

However, it offers you nothing special except that $matches will now contain $matches['page'] and $matches['action'] - but if you really need it, youcan use list() as shown above. 但是,它没有为您提供什么特别的功能,只是$ matches现在将包含$matches['page']$matches['action'] -但是,如果您确实需要它,可以使用如上所示的list()


Using the explode function would make me evaluate the uri segments later in the code, wich I'd like to avoid 使用explode函数将使我稍后在代码中评估uri段,但要避免

Instead of making sure the page is valid by it's character set, why not compile a list of valid pages lookup table and just use php's in_array which is much faster than a regular expression? 与其不通过字符集来确保该页面是有效的,何不编译一个有效页面查找表的列表,而只使用比正则表达式快得多的php in_array Extracting the URL segments into an array will allow you to have multi-dimension arrays validated in no time. 将URL段提取到数组中将使您可以立即验证多维数组。

I think the regex needs to be surrounded by a symbol; 我认为正则表达式必须用符号包围; for example '@(page(/<action>))@' . 例如'@(page(/<action>))@' See the examples in http://php.net/preg_match 请参阅http://php.net/preg_match中的示例

If you absolutely must use a regex for some reason try this: 如果出于某些原因绝对必须使用正则表达式,请尝试以下操作:

$uri = 'about/us';
$regex = '~(?<path>[^/]+)/(?<action>.+)~';
var_dump($matches);

Which outputs: 哪个输出:

array(5) {
  [0]=>
  string(8) "about/us"
  ["path"]=>
  string(5) "about"
  [1]=>
  string(5) "about"
  ["action"]=>
  string(2) "us"
  [2]=>
  string(2) "us"
}

However this will only work when the path component only goes down one level! 但是,这仅在路径组件仅下降一级时才有效!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM