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

CommentFileSizeAuthor
#64 2753591-nr-bot.txt144 bytesneeds-review-queue-bot
#61 interdiff_2753591_60-61.txt1.58 KBankithashetty
#61 2753591-61.patch3.95 KBankithashetty
#60 2753591-60.patch3.87 KBkarishmaamin
#52 fix-mismatch-2753591-52.patch3.83 KBbaikho
#49 2753591-base-url-49.patch3.9 KBNickDickinsonWilde
#42 interdiff-2753591-37_41.txt2.04 KBandypost
#42 fix_mismatch_2753591_41.patch3.9 KBandypost
#41 fix_mismatch_2753591_40.patch3.9 KBandypost
#41 interdiff-2753591-37_40.txt1.97 KBandypost
#37 interdiff-2753591-35-37.txt1.14 KBaleevas
#37 fix_mismatch_2753591_37.patch3.97 KBaleevas
#35 fix-mismatch-2753591-35.patch4.02 KBpk188
#29 fix-mismatch-2753591-29.patch3.97 KBtoomanypets
#25 fix-mismatch-2753591-25.patch3.8 KBm1r1k
#18 fix_mismatch_of-2753591-18.patch1.74 KBtoomanypets
#10 fix_mismatch_of-2753591-9.patch1.97 KBtoomanypets
#2 fix_mismatch_of-2753591-2.patch902 bytesFabianx

Issue fork drupal-2753591

Command icon 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:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Fabianx created an issue. See original summary.

Fabianx’s picture

Status: Needs work » Needs review
FileSize
902 bytes

Here 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.

Status: Needs review » Needs work

The last submitted patch, 2: fix_mismatch_of-2753591-2.patch, failed testing.

andypost’s picture

dawehner’s picture

Issue tags: +Needs tests
toomanypets’s picture

Progress! 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.

toomanypets’s picture

Unfortunately, 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:

<link rel="shortcut icon" href="/core/misc/favicon.ico" type="image/vnd.microsoft.icon" />
HREF should be "/d8/core/misc/favicon.ico"

<link rel="stylesheet" href="/sites/default/files/css/css_P31p..." media="all" />
HREF should be "/d8/sites/default/files/css/css_P31p..."

<script src="/core/assets/vendor/modernizr/modernizr.min.js?v=3.3.1"></script>
SRC should be "/d8/core/assets/vendor/modernizr/modernizr.min.js?v=3.3.1">

<script src="/sites/default/files/js/js_BKc..."></script>
SRC should be "/d8/sites/default/files/js/js_BKc..."

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

RewriteRule ^$ d8/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ d8/$1 [L]

- 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/

Fabianx’s picture

#8: I agree ... It _is_ a mess. :/

toomanypets’s picture

Until this mess is sorted, the attached patch will allow you to:

  1. Install Drupal in webroot/ (default)
  2. Install Drupal in webroot/subdir1/
  3. Install Drupal in webroot/subdir1/ and mask (hide) subdir1/ with an .htaccess file in webroot/ (See note 1 below)
  4. Install Drupal in webroot/subdir1/subdir2/
  5. Install Drupal in webroot/subdir1/subdir2/ and mask (hide) subdir1/subdir2/ with an .htaccess file in webroot/ (See note 2 below)


Notes

(1) Tested with this .htaccess file in webroot (from scenario 3 above):

# Send everything else to Drupal.
RewriteRule ^$ subdir1/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ subdir1/$1 [L]

(2) Tested with this .htaccess file in webroot (from scenario 5 above):

# Send everything else to Drupal.
RewriteRule ^$ subdir1/subdir2/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ subdir1/subdir2/$1 [L]

(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.

Fabianx’s picture

Status: Needs work » Needs review

Lets see how bad this fails.

toomanypets’s picture

My expectations are exceedingly low.

Fabianx’s picture

It passed! I am not really sure what the reasoning behind the core/install.php and core/update.php special casing is ...

catch’s picture

Component: base system » request processing system
toomanypets’s picture

Regarding #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.

znerol’s picture

In 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.

  1. +++ b/core/lib/Drupal/Core/DrupalKernel.php
    @@ -1028,33 +1028,16 @@ protected function initializeSettings(Request $request) {
       protected function initializeRequestGlobals(Request $request) {
    -    global $base_url;
    -    // Set and derived from $base_url by this function.
    -    global $base_path, $base_root;
    +    global $base_path, $base_root, $base_url;
    

    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.

  2. +++ b/core/lib/Drupal/Core/DrupalKernel.php
    @@ -1028,33 +1028,16 @@ protected function initializeSettings(Request $request) {
    +    if (substr($request->getBasePath(), -5) == '/core') {
    +      $base_path = substr($request->getBasePath(), 0, strlen($request->getBasePath()) - 4);
         }
         else {
    -      $base_path = '/';
    +      $base_path = $request->getBasePath() . '/';
         }
    

    We might want to switch the logic here, first assign $base_path unconditionally and then remove the core suffix if necessary.

  3. +++ b/core/lib/Drupal/Core/DrupalKernel.php
    @@ -1509,3 +1492,4 @@ protected function addServiceFiles(array $service_yamls) {
    +
    

    No.

znerol’s picture

Oh, and thanks for the list in #8, this is really helpful.

toomanypets’s picture

Patch modified per znerol's review in #16.

Status: Needs review » Needs work

The last submitted patch, 18: fix_mismatch_of-2753591-18.patch, failed testing.

The last submitted patch, 18: fix_mismatch_of-2753591-18.patch, failed testing.

toomanypets’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
toomanypets’s picture

The 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.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Wim Leers’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

This still needs test coverage.

m1r1k’s picture

Status: Needs work » Needs review
FileSize
3.8 KB
Fabianx’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs tests

RTBC, tests look good to me.

dawehner’s picture

+++ b/core/lib/Drupal/Core/DrupalKernel.php
@@ -1046,32 +1046,15 @@ protected function initializeSettings(Request $request) {
-      // Remove "core" directory if present, allowing install.php,
-      // authorize.php, and others to auto-detect a base path.
-      $core_position = strrpos($dir, '/core');
-      if ($core_position !== FALSE && strlen($dir) - 5 == $core_position) {
-        $base_path = substr($dir, 0, $core_position);
-      }
...
+    $base_path = $request->getBasePath();
+    if (substr($base_path, -5) == '/core') {
+      $base_path = substr($base_path, 0, strlen($base_path) - 5);

Let's keep the existing documentation.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
  1. +++ b/core/lib/Drupal/Core/DrupalKernel.php
    @@ -1046,32 +1046,15 @@ protected function initializeSettings(Request $request) {
    -    // Set and derived from $base_url by this function.
    ...
    -      // Remove "core" directory if present, allowing install.php,
    -      // authorize.php, and others to auto-detect a base path.
    

    I agree with @dawehner we shouldn't remove the comments.

  2. +++ b/core/tests/Drupal/Tests/Core/DrupalKernel/DrupalKernelTest.php
    @@ -13,6 +13,8 @@
    +    protected $backupGlobals = FALSE;
    +
    

    Why do we set this for all the tests? At the very least this needs documenting.

  3. The fix looks good and brings some consistency - it's nice to be use a less complex code path here. I think the test coverage tests everything - it'd be nice to see a test-only patch on the issue.
toomanypets’s picture

Status: Needs work » Needs review
FileSize
3.97 KB

Three of the four comments in initializeRequestGlobals(Request $request) are no longer applicable, and the fourth needed some clarification.

Fabianx’s picture

Status: Needs review » Needs work

Changes look good, but points 2/3 of #28 are still outstanding.

toomanypets’s picture

@m1r1k - Can you address points 2 and 3 of #28?

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Chris Burge’s picture

Patch #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.

Mile23’s picture

Issue tags: +Needs reroll

#29 no longer applies.

Also still needs to address #28.

pk188’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
FileSize
4.02 KB

Rerolled.

Status: Needs review » Needs work

The last submitted patch, 35: fix-mismatch-2753591-35.patch, failed testing. View results

aleevas’s picture

Status: Needs work » Needs review
FileSize
3.97 KB
1.14 KB

Small fix for the #29 with notices from #28
Please check

Status: Needs review » Needs work

The last submitted patch, 37: fix_mismatch_2753591_37.patch, failed testing. View results

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

andypost’s picture

andypost’s picture

The method name in test was the issue

andypost’s picture

The last submitted patch, 41: fix_mismatch_2753591_40.patch, failed testing. View results

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Wim Leers’s picture

I 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.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

NickDickinsonWilde’s picture

Re-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.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

baikho’s picture

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

jherencia’s picture

We'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.

effulgentsia’s picture

@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.

jherencia’s picture

Thank you @effulgentsia.

We also had to create symlinks inside of the web site document roots to map the suffixes.

...

We made Drupal think it’s installed in a subdirectory. This way, it was including the country suffix in the absolute URLs it generated:

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 :)

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

zterry95’s picture

Hi @jherencia, Have you found solution for this #54?

Spokje’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll
karishmaamin’s picture

Re-rolled patch against 9.4.x.

ankithashetty’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
FileSize
3.95 KB
1.58 KB

Fixed 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:

 172 | ERROR | [ ] If the line declaring an array spans longer than 80 characters, each element should be broken into its own line
 174 | ERROR | [ ] If the line declaring an array spans longer than 80 characters, each element should be broken into its own line
 175 | ERROR | [x] Array closing indentation error, expected 4 spaces but found 2

Thanks!

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
FileSize
144 bytes

The 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.

voleger made their first commit to this issue’s fork.

voleger’s picture

andypost’s picture

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: -Needs tests +Needs Review Queue Initiative

#45 tagged for an issue summary update which still needs to happen it appears.

andypost’s picture

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.