开发者

PHP & Apache - parse mod_rewrite url

开发者 https://www.devze.com 2022-12-19 22:07 出处:网络
I have my server\'s apache configured to redirect every request into index.php, so that I can use urls like :

I have my server's apache configured to redirect every request into index.php, so that I can use urls like :

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. 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 matc开发者_高级运维h "_-azAZ09", something like this. 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. Using the explode function would make me evaluate the uri segments later in the code, wich I'd like to avoid. If there's no match to the uri, immediatelly forward the page to a 404 one and break the operation. Don't know if I made myself clear ..


Instead of using regular expressions, why not simply split the incoming 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.


Using the explode function would make me evaluate the uri segments later in the code, wich I'd like to avoid

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? Extracting the URL segments into an array will allow you to have multi-dimension arrays validated in no time.


I think the regex needs to be surrounded by a symbol; for example '@(page(/<action>))@'. See the examples in 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!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号