Home > Software engineering >  Serving a static export of NextJS inside a Wordpress theme
Serving a static export of NextJS inside a Wordpress theme

Time:10-20

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.

CodePudding user response:

Not exactly sure what all you need, but I think this pair of rules works on the above test cases:

RewriteRule ^$ /wp-content/themes/mytheme/next/index.html [L]
RewriteRule ^(. [^/])/?$ /wp-content/themes/mytheme/next/$1.html [L]

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.

CodePudding user response:

Adding the following seemed to work:

RewriteRule ^(.*)/([^\.] )$ /wp-content/themes/mytheme/next/$1/$2.html [L]

So:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)/([^\.] )$ /wp-content/themes/mytheme/next/$1/$2.html [L]
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>

Doesn't feel like the best solution but c'est la vie.

  • Related