wecodemore / wpstarter

Easily bootstrap whole site Composer packages for WordPress.
https://wecodemore.github.io/wpstarter/
MIT License
246 stars 35 forks source link

`wp server` correct `WP_HOME` value should be retrieved from `$_SERVER HTTP_HOST`, not `SERVER_NAME` #101

Open lkraav opened 5 years ago

lkraav commented 5 years ago

Describe the bug

If we run our web server on an alternate port, like 8080, let's say using wp server, then $_SERVER SERVER_NAME will not provide the correct WP_HOME calculation value, but $_SERVER HTTP_HOST does.

print_r( $_SERVER );

Array
(
    ...
    [SERVER_NAME] => localhost
    [SERVER_PORT] => 8080
    [REQUEST_URI] => /
    [HTTP_HOST] => localhost:8080
    [SERVER_ADDR] => 127.0.0.1
    [WP_ENV] => development
    ...
)

Alternatively, $_SERVER SERVER_PORT value maybe also has to be used in WP_HOME auto-calculation.

To Reproduce

  1. Run webserver on a non-standard port, like 8080
  2. Use get_theme_file_uri() to calculate an enqueue asset URL
  3. Watch 404 errors for enqueued assets

Expected behavior Port value recognized.

gmazzap commented 5 years ago

Even this is true in some cases, the value of $_SERVER widely change depending on the web-server, proxy, etc. So no auto-calculation is really trustable.

Moreover, $_SERVER values that start with HTTP_ can be overridden via headers, meaning that a client can override those (and that could have security implications).

This is why it is usually preferred to avoid them, and rely on values like SERVER_NAME that depends on server configuration.

This is also why is highly suggested to manually set WP_HOME: auto-calculation is not really trustable here.

That said, looking at SERVER_PORT is something we could do, because it is little effort and can be useful.

lkraav commented 5 years ago

I agree, patching SERVER_PORT is probably the way to go here.

lkraav commented 5 years ago

This issue stems a bit from https://github.com/wecodemore/wpstarter/issues/100

We could configure our local staging port values dynamically via something like wp server --port 8081, without needing to mod the committed .env template. It's nearly the only thing we need to mod.

gmazzap commented 2 years ago

Please check if 4c76d9d solved it. Thanks.

lkraav commented 1 year ago

Please check if 4c76d9d solved it. Thanks.

I tested with latest dev build today, and something is still going wrong. Without explicitly setting WP_HOME, a ton of errors are output:

Notice: Undefined index: host in wpstarter.git/vendor/wp-cli/server-command/router.php on line 77

I'm serving with wp server --docroot=public.

gmazzap commented 1 year ago

@lkraav, I think you have to pass the URL to WP CL with the --url parameter.

WP Starter is designed to guess the URL in a web server environment.

When you're on CLI, $_SERVER only contains env variables, nothing that can help WP Starter determine the URL correctly.

The best thing you can do is tell WP CLI which URL you want to "emulate".

gmazzap commented 1 year ago

@lkraav with 1cb6f60 wp server seems to work for me.

Can you confirm?

gmazzap commented 1 year ago

Can I close this?