Apache: How can I redirect to a subfolder with a URL param but serve required content via the main URL?

18 views Asked by At

I want to host the WordPress Playground on my own server: https://wordpress.github.io/wordpress-playground/architecture/host-your-own-playground/

For that, I've created a subdomain playground.my-domain.com. After following the instructions, I was able to see the WordPress Playground loading. That was a great success so far.

But now it gets interesting. My goal is it to have multiple folders inside the document root of the URL which are named as my plugins I want to load a demo for e.g. my-plugin.

So I've created a folder my-plugin within the document root and moved every file inside there. Now I've had the idea to create a .htaccess file inside the root dir which processes the URL e.g. playground.my-domain.com/my-plugin/:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /

# If the request is for an existing file or directory, serve it directly
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

# Redirect requests to the correct subfolder based on the URL parameter
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteCond %{QUERY_STRING} ^folder=([^&]+)$
RewriteRule ^ %{QUERY_STRING}/%1%{REQUEST_URI} [L]

# Rewrite requests to the subfolder

# If the requested resource doesn't exist, return a 404 error
RewriteRule ^ - [R=404]

This works since I can see that this serves the content of my subfolder. But now I got some problems. Inside the index.html file, there are some assets which are loaded:

<script type="module" crossorigin src="/assets/index-57a155ca.js"></script>
<link rel="modulepreload" crossorigin href="/assets/modulepreload-polyfill-3cfb730f.js">
<link rel="modulepreload" crossorigin href="/assets/preload-helper-cf010ec4.js">
<link rel="stylesheet" href="/assets/index-d60708af.css">

After checking the dev console, I can see, that the server / browser is trying to serve these files from the main URL which indeed can not work since they are located inside the subfolder:

GET https://playground.my-domain.com/assets/index-d60708af.css net::ERR_ABORTED 404 (Not Found)

Theoretically, I need to change the host root directly to tell the server to search inside the subfolder, but I'm not able to get this working. I would be helpful for any idea!

0

There are 0 answers