putyourlightson / craft-blitz

Intelligent static page caching for creating lightning-fast sites with Craft CMS.

Home Page:https://putyourlightson.com/plugins/blitz

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Server rewrite does not work for the primary site without a subfolder

Romanavr opened this issue · comments

Bug Report

This is something very similar to #351 and maybe #562.

I have multiple sites setup with a subdomains, for example:
https://site.com/en — Primary website, also available by the https://site.com
https://site.com/de — Secondary

When visiting sites with a subdirectory at the end, the rewrite works as expected.
https://site.com/en, https://site.com/de do not have
<-- Served by Blitz on XXX -->

However, if I go to the primary site without /en/ at the end (https://site.com), the rewrite will fall off, I can see:
<-- Served by Blitz on XXX -->

I added a header to check what the actual cache path is used when visting a primary site without a subfolder at the end.
Without subfolder
testHeader: /cache/blitz/site.com//index.php//index.html
With subfolder
testHeader: /cache/blitz/site.com//en//index.html

I'm not sure what's wrong. My server configuration is wrong or rewrite is not working correctly, but I think rewrite should work everywhere?

My rewrite (for NGINX):

    #- Blitz cache rewrite
    set $cache_path false;
    if ($request_method = GET) {
        set $cache_path /cache/blitz/$host/$uri/$args/index.html;
    }
    if ($args ~ "token=") {
        set $cache_path false;
    }

    #- Send would-be 404 requests to the cache path or Craft
    location / {
        try_files $cache_path $uri $uri/ /index.php?$query_string;
    }
    #- Blitz cache rewrite end

Plugin Version

4.9.3

Craft CMS Version

4.5.11.1

PHP Version

8.1.25

How exactly have you made it so that the bare domain, site.com, displays the primary site?

How exactly have you made it so that the bare domain, site.com, displays the primary site?

Sorry for the reply,
I think this is due to the fact that my @baseUr & @web aliases are set without a subdomain.

...
'aliases'             => [
            '@baseUrl'  => App::env('BASE_URL'), // https://site.com/ instead of https://site.com/en/
             '@web'      => App::env('BASE_URL'),
...

My guess is that Blitz doesn't recognise the base URL as belonging to any of the existing sites, and therefore none of the included URI patterns are being matched. Perhaps you should add a redirect from site.com to site.com/en, which should resolve this issue and also sounds like a good idea from an SEO perspective.