How to create WordPress in a subdirectory of Next JS application
<p>Dear community, Hello,</p>
<p>My web application uses next JS under the domain name domain.com</p>
<p>Now I want to create a wordpress blog (wordblog.com/community) under example.com/community</p>
<p>Using Next JS Rewrites, I added this to my next configuration: </p>
<pre class="brush:php;toolbar:false;">async rewrites() {
return {
fallback: [
{
source: "/community/:path*",
destination: `https://wordblog.com/community/:path*`,
},
],
}
},</pre>
<p>In my WordPress installation folder. </p>
<p>1- Add it to .htacces</p>
<pre class="brush:php;toolbar:false;"><IfModule mod_headers.c>
<FilesMatch "\.(ttf|ttc|otf|eot|woff|woff2|font.css|css|js)$">
Header set Access-Control-Allow-Origin "*"
</FilesMatch>
</IfModule></pre>
<p>2-Add this to wp-config.php: </p>
<pre class="brush:php;toolbar:false;">define('WP_SITEURL', 'https://wordblog.com/community');
define('WP_HOME', 'https://example.com/community');
define('COOKIE_DOMAIN', '.example.com');</pre>
<p>3-Added this wp-content/themes/your-theme/functions.php</p>
<pre class="brush:php;toolbar:false;">remove_filter('template_redirect','redirect_canonical');
add_filter('rest_url', 'serve_rest_url_on_wp_subdomain');
function serve_rest_url_on_wp_subdomain ($url) {
return str_replace('https://example.com/community', 'https://wordblog.com/community', $url);
}</pre>
<p>But when I visit domain.com/community, it redirects me to wordblog.com/community. </p>
<p>In addition, some pages of wordblog.com/community are rendered under wordblog.com/community/pageX instead of domain.com/community/pageX</p>