Follow-up to #2528988: Remove the option to specify a base_url from within settings.php
Problem/Motivation
- $base_url is not the same as $base_root . $request->getBasePath() in Symfony.
- This leads to lots of problems with TrustedRedirectResponse() as it thinks we redirect to an external path.
- Therefore #2528988: Remove the option to specify a base_url from within settings.php did not reach its goal to unify both approaches, but made it in fact almost impossible to work-around the mismatch
Proposed resolution
- Use Symfony function to find the script instead of $_SERVER['SCRIPT_NAME']
Remaining tasks
- Do it
Comment | File | Size | Author |
---|
Issue fork drupal-2753591
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
Fabianx CreditAttribution: Fabianx as a volunteer and at Tag1 Consulting commentedHere is a patch.
Note that getBaseUrl() while technically the correct replacement is very weirdly named in Symfony.
We could also think about just calling getBasePath() without the dirname() etc.
Comment #4
andypostThis affects #1543858: Add a startup configuration for the built-in PHP server that supports clean URLs
Comment #5
dawehnerComment #6
toomanypets CreditAttribution: toomanypets commentedProgress! Thank you Fabianx.
The patch from #2 allows me to run d8 from a subdirectory of webroot while masking (hiding) the subdirectory from the URL though an .htaccess file in webroot. See https://www.drupal.org/node/2528988#comment-11313379 for a complete description of the use case, configuration, and associated problems.
I'll report any unexpected side effects.
Comment #7
toomanypets CreditAttribution: toomanypets commentedUnfortunately, this breaks the case where you don't wish to mask the installation subdirectory in the URL.
Scenario A (PASS)
- Webroot is
/home/user/sites/www.example.com/htdocs/
- D8 installed in
/home/user/sites/www.example.com/htdocs/d8/
- There is not an .htaccess file in webroot
- The .htaccess file in
/home/user/sites/www.example.com/htdocs/d8/
has not been modified- The patch from #2 has not been applied
- Everything works as expected when visiting
http://www.example.com/d8/
Scenario B (FAIL)
- Webroot is
/home/user/sites/www.example.com/htdocs/
- D8 installed in
/home/user/sites/www.example.com/htdocs/d8/
- There is not an .htaccess file in webroot
- The .htaccess file in
/home/user/sites/www.example.com/htdocs/d8/
has not been modified- The patch from #2 has been applied
- When visiting
http://www.example.com/d8/
the links are correct, but the paths to css and js files are incorrect:Scenario C (PASS)
- Webroot is
/home/user/sites/www.example.com/htdocs/
- D8 installed in
/home/user/sites/www.example.com/htdocs/d8/
- There is an .htaccess file in webroot
- The .htaccess file in
/home/user/sites/www.example.com/htdocs/d8/
has not been modified- The patch from #2 has been applied
- Everything works as expected when visiting
http://www.example.com/
Comment #8
toomanypets CreditAttribution: toomanypets commentedThis is a mess. From a quick search of open 8.x issues...
#2528988: Remove the option to specify a base_url from within settings.php
#2070185: Unify global $base_path and the symfony base_url from the request context.
#2529170: Remove DrupalKernel::initializeRequestGlobals and replace base_root, base_url and base_path with a service
#1433996: update.php doesn't work when Drupal is installed in a subfolder and RewriteBase is used
#2341553: Use routing to generate update.php url
#2377233: $_SERVER['REQUEST_URI'] reverse proxies, base_urls and Apache2 don't seem to mix well
#2403967: DrupalKernel should setup the request context
#2404601: Figure out how to best deal with RequestContext::setCompleteBaseUrl
#2568773: Add $options['base_url'] support to the unrouted URL assembler
#2488514: D8: trusted_host_patterns should include base URL?
#2548095: Add option to Url() to force the site base_url to be used
As a side note, we also have special handling for install.php and rebuild.php in Drupal root's .htacess file.
Comment #9
Fabianx CreditAttribution: Fabianx as a volunteer and at Tag1 Consulting commented#8: I agree ... It _is_ a mess. :/
Comment #10
toomanypets CreditAttribution: toomanypets commentedUntil this mess is sorted, the attached patch will allow you to:
Notes
(1) Tested with this .htaccess file in webroot (from scenario 3 above):
(2) Tested with this .htaccess file in webroot (from scenario 5 above):
(3) With scenarios 3 and 5...
- When accessing install.php, the subdirectory(s) are not masked in the URL.
- When accessing install.php or update.php,viewing source in the browser reveals the install path for all links (css, js, etc.).
- You must clear the cache (or drush cr ) after the installation.
None of these affect the functionality of the site, and the minor issues with install.php and update.php are expected given the other known, related issues.
I also tested with Pathauto and Redirect installed and configured -- no problems.
Comment #11
Fabianx CreditAttribution: Fabianx as a volunteer and at Tag1 Consulting commentedLets see how bad this fails.
Comment #12
toomanypets CreditAttribution: toomanypets commentedMy expectations are exceedingly low.
Comment #13
Fabianx CreditAttribution: Fabianx as a volunteer and at Tag1 Consulting commentedIt passed! I am not really sure what the reasoning behind the core/install.php and core/update.php special casing is ...
Comment #14
catchComment #15
toomanypets CreditAttribution: toomanypets commentedRegarding #13...
Calls to /somepath/core/authorize.php, /somepath/core/install.php, and /somepath/core/rebuild.php are made directly -- they bypass the front controller (/somepath/index.php). In these cases, Symfony reports the base path as /somepath/core instead of /somepath. So, if the last five characters of $request->getBasePath() are '/core' we chomp that bit off to set the the globals ($base_path, $base_url, etc.).
We don't need to special case update.php because it is located in Drupal's base directory, not in /somepath/core.
Comment #16
znerol CreditAttribution: znerol commentedIn the long run we want to remove those globals. But if we need to fix
$base_url
in the meantime, then let's do it.This looks like you want to introduce a new global on first sight. For the sake of reviewers sanity, please let's not mix functional and cosmetic changes.
We might want to switch the logic here, first assign
$base_path
unconditionally and then remove thecore
suffix if necessary.No.
Comment #17
znerol CreditAttribution: znerol commentedOh, and thanks for the list in #8, this is really helpful.
Comment #18
toomanypets CreditAttribution: toomanypets commentedPatch modified per znerol's review in #16.
Comment #21
toomanypets CreditAttribution: toomanypets commentedComment #22
toomanypets CreditAttribution: toomanypets commentedThe test failures in shown in comments #19 and #20 are examples of #2749955: Random fails in UpdatePathTestBase tests. A retest of the #18 patch returned no errors.
Comment #24
Wim LeersThis still needs test coverage.
Comment #25
m1r1k CreditAttribution: m1r1k commentedComment #26
Fabianx CreditAttribution: Fabianx as a volunteer and at Tag1 Consulting commentedRTBC, tests look good to me.
Comment #27
dawehnerLet's keep the existing documentation.
Comment #28
alexpottI agree with @dawehner we shouldn't remove the comments.
Why do we set this for all the tests? At the very least this needs documenting.
Comment #29
toomanypets CreditAttribution: toomanypets commentedThree of the four comments in initializeRequestGlobals(Request $request) are no longer applicable, and the fourth needed some clarification.
Comment #30
Fabianx CreditAttribution: Fabianx as a volunteer and at Tag1 Consulting commentedChanges look good, but points 2/3 of #28 are still outstanding.
Comment #31
toomanypets CreditAttribution: toomanypets commented@m1r1k - Can you address points 2 and 3 of #28?
Comment #33
Chris Burge CreditAttribution: Chris Burge commentedPatch #29 resolves an issue I was having when trying to access "/user" and getting "Redirects to external URLs are not allowed by default, use \Drupal\Core\Routing\TrustedRedirectResponse for it." +1 for getting this issue wrapped up and code committed.
Comment #34
Mile23#29 no longer applies.
Also still needs to address #28.
Comment #35
pk188 CreditAttribution: pk188 at OpenSense Labs commentedRerolled.
Comment #37
aleevasSmall fix for the #29 with notices from #28
Please check
Comment #40
andypostComment #41
andypostThe method name in test was the issue
Comment #42
andypostFix for provider as well
Comment #45
Wim LeersI though this was causing #2826137, but having tried it in detail (see #2826137-22: CDN is serving files with wrong path), this seems to work fine:
$request->getSchemeAndHttpHost() . $request->getBasePath() === $GLOBALS['base_url']
, both when D8 is installed in the root of a domain and in a subdirectory.So it's not clear to me at all what the state of this issue is. Can we get a failing test, i.e. a test that shows what the problem is in HEAD? The steps to reproduce in #7 no longer work because CSS/JS asset URLs are now always root-relative, since #1494670: References to CSS, JS, and similar files should be root-relative URLs: avoids mixed content warnings & fewer bytes to send.
Comment #49
NickDickinsonWildeRe-roll, same contents except for line numbers really, but composer was being tetchy about it.
Will try to detail full use case for at least one required situation tomorrow.
Comment #52
baikhoReroll of #49.
Comment #54
jherencia CreditAttribution: jherencia commentedWe've trying to have a proxy (nginx) that redirects the traffic to several applications:
- mydomain.com => a nextjs app that uses Drupal content to render the frontend application
- mydomian.com/drupal => the D9 site
We do not see any "easy and no hacky" way to tell D9 that it's under a subfolder and behind a proxy.
I think this is the issue that might fix this, is there any chance that these patches go further?
Thank u in any case for the great product and effort.
Comment #55
effulgentsia CreditAttribution: effulgentsia at Acquia commented@jherencia: Is the D9 site within a "drupal/" URL prefix on the origin server? If not, can you make it be using something like the trick in https://medium.com/@lmakarov/drupal-8-and-reverse-proxies-the-base-url-d...? If it is, then it should work just fine without any of the patches that are in this issue, unless I'm misunderstanding your setup.
Comment #56
jherencia CreditAttribution: jherencia commentedThank you @effulgentsia.
This is what I called "little tricky / hacky" way, and it didn't work either. I prefer not to share the configuration details in this thread as it would make noise to other people.
I think Drupal should have compatibility with "not so common" configurations. Thank you anyway :)
Comment #58
zterry95 CreditAttribution: zterry95 at DAVYIN Internet Solutions / 戴文信息科技有限公司 commentedHi @jherencia, Have you found solution for this #54?
Comment #59
SpokjeComment #60
karishmaamin CreditAttribution: karishmaamin at Specbee commentedRe-rolled patch against 9.4.x.
Comment #61
ankithashettyFixed custom command failure errors in #60.
On fixing this
170 | ERROR | [x] Line indented incorrectly; expected 4 spaces, found 2
, the following errors popped up:Thanks!
Comment #64
needs-review-queue-bot CreditAttribution: needs-review-queue-bot as a volunteer commentedThe Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".
Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #67
volegerRerolled #61
Comment #68
andypostUsing MR and looks really
Comment #69
smustgrave CreditAttribution: smustgrave at Mobomo commented#45 tagged for an issue summary update which still needs to happen it appears.
Comment #70
andypost