Symfony 6.1 not matching routes with apache rewriting

Viewed 36

I'm running in to an issue on after deploying my website on a shared hosting. It's a React frontend with a Symfony 6.1 REST API. The problem is that it's currently hosted on a subdomain beta.example.com and in order to work I'm using Apache to rewrite the subdomain to a sub directory (/beta/) while rewriting the example.com domain to /master/. The root .htaccess file looks like this:

# Root /.htaccess
RewriteEngine On
RewriteBase /

# Beta subdomain
RewriteCond %{HTTP_HOST} ^beta\.example\.com [NC]
RewriteCond %{REQUEST_URI}  !^/beta/
RewriteRule ^(.*)$ /beta/$1 [L,QSA]

# Redirect other invalid subdomains to example.com
RewriteCond %{HTTP_HOST} !^beta\.example\.com [NC]
RewriteCond %{HTTP_HOST} ^.+\.(.*)\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1.%2/$1 [R=301,L]

# Rewrite the apex domain (example.com) to /master/
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{REQUEST_URI}  !^/master/
RewriteRule ^(.*)$ /master/$1 [L,QSA]

This works just fine for both the apex and beta subdomain. In the /beta/ sub directory are the files for React (Next.js) and that requires it's .htaccess file for rewriting. Also in the directory are the Symfony files and directories like src/, vendor/, var/, .env, composer.json etc. as well as the public directory which I have renamed to api/ so I can send request from my React app to the API like /api/login and /api/nav. The React's .htaccess file looks like this:

# /beta/.htaccess
Options -MultiViews

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.html$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteCond %{REQUEST_URI}  !^(?:\/[a-z]+)?\/api\/ [NC]
    RewriteRule . /index.html [L]
</IfModule>

The React app works fine. Routes are being rewritten correctly to the index.html file, but all API requests from the app return 404 Not found. Important fact to note is that it's not a generic 404 but Symfony's 404 exception indicating that the request did actually reach the /api/index.php file but the route was not matched. Which is strange because the routes exist and they work on my local dev environment.

The /beta/api/ contains the standard Symfony index.php file and an .htaccess file created by the symfony/apache-pack which contains this:

# /beta/api/.htaccess
DirectoryIndex index.php

<IfModule mod_negotiation.c>
    Options -MultiViews
</IfModule>

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{REQUEST_URI}::$0 ^(/.+)/(.*)::\2$
    RewriteRule .* - [E=BASE:%1]

    RewriteCond %{HTTP:Authorization} .+
    RewriteRule ^ - [E=HTTP_AUTHORIZATION:%0]

    RewriteCond %{ENV:REDIRECT_STATUS} =""
    RewriteRule ^index\.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ %{ENV:BASE}/index.php [L]
</IfModule>

<IfModule !mod_rewrite.c>
    <IfModule mod_alias.c>
        RedirectMatch 307 ^/$ /index.php/
    </IfModule>
</IfModule>

If I manually navigate to one of my routes https://beta.example.com/api/nav I get the same Symfony 404 exception I get in the React app. But if i go to https://beta.example.com/beta/api/nav (including the /beta/ subdirectory) I get the correct response. So it seems the problem is that Symfony expects the full /beta/api/nav route but that does not match the request route /api/nav. The problem is that the request route Symfony recieves inside the /beta/api/ directory is actually /api/nav instead of /nav which would match the route. I can't figure out why this happens. Somehow using the subdomain rewrite from the root .htaccess file causes this issue.

I've spent a lot of time trying to fix this and I couldn't find anything about it online. Has anyone encountered the same problem?

0 Answers
Related