fbctf icon indicating copy to clipboard operation
fbctf copied to clipboard

Pretty URLs

Open Daniel15 opened this issue 7 years ago • 6 comments

Is it possible to get pretty URLs - For example /game rather than /index.php?p=game? The fact that the site is built with PHP is an implementation detail that shouldn't necessarily be reflected in URLs 😄

Daniel15 avatar Oct 04 '16 18:10 Daniel15

From an email thread, some options are:

  • nikic/fastroute is a popular option in the PHP community, with hack typechecker support via .hhi; used by the prod version of docs.hhvm.com
  • fredemmott/hack-router is a fork of the routing code from docs.hhvm.com to turn it into a re-usable library, built on top of fastroute; used in the next version of docs.hhvm.com
  • fredemmott/hack-router-codegen adds auto-generation of URI-Maps, URI builders, and parameter structs to this; also used in docs.hhvm.com (documentation severely lacking, I'll fix that soon - https://github.com/hhvm/user-documentation/commit/bc35dcbc475002e719c4e5b3a76a5369d43dc7f2 shows how to just use the router/urimap generation)

Also, src/ should not be the www docroot; strongly recommend moving src/index.php and static/ to a new public/ folder instead, and making all endpoints only accessible through index.php via pretty urls.

Easiest way when using HHVM's built-in webserver is to configure hhvm.server.default_document and hhvm.server.error_document404 to point to index.php; not sure what would be required for your nginx setup (though you can probably switch to the built-in webserver anyway and simplify the configuration).

fredemmott avatar Oct 05 '16 15:10 fredemmott

not sure what would be required for your nginx setup

If you're using Nginx, the easiest solution is probably to do:

try_files $uri /index.php?p=$uri

This will try to load the URI directly if it exists (ie. a static file), or rewrite/"internally redirect" to index.php if it doesn't exist (so /game would go to /index.php?p=game)

Adding a new routing library may be overkill since index.php is already handling routing (unless you want to remove that and instead use one of the routers mentioned by @fredemmott)

Easiest way when using HHVM's built-in webserver

I was going to say "wasn't that deprecated?" but it looks like it was replaced with a proxygen-based server at some point. My knowledge is outdated 😛

Daniel15 avatar Oct 05 '16 17:10 Daniel15

Adding a new routing library may be overkill since index.php is already handling routing (unless you want to remove that and instead use one of the routers mentioned by @fredemmott)

Yeah, depends on if you want to have /foo/?bar=somevalue or /foo/somevalue; if the former, a library might be overkill, if the latter, no point reinventing it.

proxygen

The proxygen http server's been recommend over FastCGI since 3.9 :)

fredemmott avatar Oct 05 '16 17:10 fredemmott

The proxygen http server's been recommend over FastCGI since 3.9

Cool, good to know 😄 I was having various segfault issues when trying to switch over to HHVM (https://github.com/facebook/hhvm/issues/6498 but also others I couldn't figure out the cause for) so I ended up switching to PHP7 instead, except for my sites that use Hack. Maybe I should try switching back.

Daniel15 avatar Oct 05 '16 17:10 Daniel15

FYI hack-router and hack-router-codegen are now part of hhvm/, not fredemmott/

fredemmott avatar May 23 '17 15:05 fredemmott

This should probably be on hold until we're on 3.23 or above; it's now likely there'll be some fairly major changes to the libraries between 3.23 and 3.24

fredemmott avatar Sep 26 '17 15:09 fredemmott