Home  >  Q&A  >  body text

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>
P粉006540600P粉006540600440 days ago476

reply all(1)I'll reply

  • P粉154798196

    P粉1547981962023-08-30 12:25:32

    You may be able to use the Alias ​​directive in your apache settings to resolve this issue: https://httpd.apache.org/docs/2.0/mod/mod_alias.html#alias

    This means you have to remove the redirect in nextJS. Apache will know to serve /community from your WordPress installation.

    reply
    0
  • Cancelreply