2

Given /group/14/ with multiviews enabled on group, I get a redirect to /group.php, but is 14 passed to PHP in any form besides the $_SERVER variables? Ideally, I could get this in a query string of some kind. I read parts of the Content Negotiation article on it, but I can't seem to find any indication that this is the case.

Edit: For whatever reason, that above wasn't clear. Let me try again.

I have group.php which wants a group id like group.php?id=14. Normally, I'd use URL rewriting to have /group/14/ rewrite to /group.php?id=14. However, in this case I have multiviews enabled, and the URL rewrite does not trigger. So /group/14/ DOES get sent to /group.php but does not send 14 as a query string. Is there anyway besides parsing 14 from the $_SERVER['REQUESTED_URI'] that I can get it with multiviews enabled?

Levi Morrison
  • 19,116
  • 7
  • 65
  • 85
  • What do you assume *besides* `$_SERVER` variables? Normally that array contains the environment variables of the server, which is what apache provides. For completeness it would be good to know as well which SAPI interface you're using (CGI, MOD_PHP,...). – hakre Sep 27 '11 at 14:43
  • Ideally I'd be passed a query string. I like the multiview functionality, but if I can't get `14` in a query or something easy to parse, then I can't use multiviews for significant portions of my website. – Levi Morrison Sep 27 '11 at 14:50
  • But what is your actual problem to read out the part from the request URI? I have one PHP site installed based on multiviews for readable URLs and it's working fine. Maybe you can outline what you try to achieve and into which problem you run in your question? From what you shared so far it looks easily solveable. – hakre Sep 27 '11 at 16:22
  • Edited my question, although really I shouldn't have to: I already asked it succinctly as the first sentence of my paragraph. . . – Levi Morrison Sep 27 '11 at 19:29
  • Jup, thanks for clarifying: The URI that get's request does not have *any* query, so you need to manually parse `$_REQUEST['URI']` on your own. If you build yourself some helper functions you can then map the part at the end onto variables. – hakre Sep 28 '11 at 18:34

1 Answers1

2

This rule will match:

RewriteRule ^group.php/(.*)$ ./group.php?id=$1 [L,NE]

Having Multiviews enabled transform the group/14 to group.php/14 (where ${PATH_INFO} is '/14', which is smarter than other $_SERVER vars, but this is another problem). After this first apache internal rewrite (from multiviews) the rewriteRule are run again, and you can then capture the group.php/14.

regilero
  • 29,806
  • 6
  • 60
  • 99
  • Is it smart to do the mapping in the `.htaccess` instead in a mapper inside PHP code? Otherwise you bind the server configuration more tight to the application logic. – hakre Sep 29 '11 at 22:08
  • @hakre While that's true, it's much easier to do with Apache than it is with PHP. And if my server config changes, I'll probably have to change my PHP that parses that out anyway, right? – Levi Morrison Sep 30 '11 at 06:05
  • @hakre: this is a whole complex question. I would add that Multiviews is quite a bad thing in term of security, that .htaccess is worst (and much slower) than a Directory section, and that anyway you need to filter out the id parameter content in your server side router. – regilero Sep 30 '11 at 07:39