I’m struggling to serve a NextJS export inside of a WordPress theme folder. I currently have the following htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^$ /wp-content/themes/mytheme/next/index.html [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/wp-content/themes/mytheme/next/
RewriteRule ^(.*)$ /wp-content/themes/mytheme/next/$1
RewriteRule ^([^.]+)$ $1.html
</IfModule>
It works for the root, as well as initial subdirectories but does not work for sub-subdirectories. It’s also not working if there’s a trailing slash i.e:
mysite.com 200 ✅ /wp-content/themes/mytheme/next/index.html
mysite.com/products 200 ✅ /wp-content/themes/mytheme/next/products.html
mysite.com/products/ 403 ❌ /wp-content/themes/mytheme/next/products.html
mysite.com/products/app 404 ❌ /wp-content/themes/mytheme/next/products/app.html
mysite.com/products/app/ 404 ❌ /wp-content/themes/mytheme/next/products/app.html
I’ve tried to turn on RewriteLog but without success. Using Mamp.
Any help, much appreciated.
2
Answers
Adding the following seemed to work:
So:
Doesn't feel like the best solution but c'est la vie.
Not exactly sure what all you need, but I think this pair of rules works on the above test cases:
The first rule matches the root since it’s different from the other paths (returning
index.html
), the second matches any URL, drops any trailing slash, and adds a.html
to the end.