1

Problem

I have a problem on a local Valet setup: the locale URL segment is appearing twice in URLs.

Eg from a page at URL of en/about/approach, {{ url('about/approach')}} outputs https://website.test/en/en/about/approach

(And you only get the correct URL if outputting this from /about/approach)

Guessing I've missed something obvious but no idea what... thanks!


Setup

Base URLs are set to @web/en/ and @web/de/.

In the web folder we have two index files, eg de/index.php and en/index.php

Running valet which reports This site is served by [CraftValetDriver].

In general.php I've got:

'test' => [
// Base site URL
'siteUrl' => 'http://website.test'

The Entry URI Format for the Pages section is {parent.slug}/{slug}


Contents of /web/en/index.php:

<?php
/**
 * Craft www bootstrap file
 */

// Set path constants
define('CRAFT_BASE_PATH', dirname(__DIR__, 2));
define('CRAFT_SITE', 'english');
define('CRAFT_VENDOR_PATH', CRAFT_BASE_PATH.'/vendor');

$root = dirname(__DIR__).'/en';

// Load Composer's autoloader
require_once CRAFT_VENDOR_PATH.'/autoload.php';

// Load dotenv?
if (file_exists(CRAFT_BASE_PATH.'/.env')) {
    (new Dotenv\Dotenv(CRAFT_BASE_PATH))->load();
}

// Load and run Craft
define('CRAFT_ENVIRONMENT', getenv('ENVIRONMENT') ?: 'production');
$app = require CRAFT_VENDOR_PATH.'/craftcms/cms/bootstrap/web.php';
$app->run();

Contents of /web/index.php:

<?php
/**
 * Craft web bootstrap file
 */

// Set path constants
define('CRAFT_BASE_PATH', dirname(__DIR__));
define('CRAFT_VENDOR_PATH', CRAFT_BASE_PATH.'/vendor');

// Load Composer's autoloader
require_once CRAFT_VENDOR_PATH.'/autoload.php';

// Load dotenv?
if (file_exists(CRAFT_BASE_PATH.'/.env')) {
    (new Dotenv\Dotenv(CRAFT_BASE_PATH))->load();
}

// Load and run Craft
define('CRAFT_ENVIRONMENT', getenv('ENVIRONMENT') ?: 'production');
$app = require CRAFT_VENDOR_PATH.'/craftcms/cms/bootstrap/web.php';
$app->run();
3

Your issue is that the @web alias resolves to this:

The URL to the folder that contains the index.php file that was loaded for the request

Now that you’re serving your English site from web/en/index.php, @web will already resolve to http://website.test/en.

The alias @web/en you are using for your site’s base URL does therefore resolve to http://website.test/en/en

So you can fix it be using @web for your base URLs, or what I would rather recommend is to serve all your sites from a single web/index.php file (which is possible now in Craft 3). With this change @web will always refer to http://website.test and you need to use @web/en again in your site settings.

  • Just a simple question: we usually do that too and I usually recommend others the same because it's the most easiest solution in my opinion. Do you know any disadvantages in having a single index.php for all requests rather than having multiple different one? – Robin Schambach Apr 27 '18 at 10:41
  • @RobinSchambach none that I know of, but I don’t have much experience with it either. I can update my answer when I encounter a situation where dedicated index.php files are needed. But the auto detection seems to work really well! – carlcs Apr 27 '18 at 10:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.