You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Nginx下WordPress默认搜索查询的Rewrite规则问题求助

Hey there, let's get that annoying search 404 issue sorted out quickly! From your example, it looks like when users initiate a search from your /blog/ path, the generated URL (http://example.com/blog/?s=searchterm) isn't pointing to the correct WordPress search endpoint, which lives at the root (http://example.com/?s=searchterm). Here's how to fix it with Nginx rewrite rules:

Step 1: Edit Your Nginx Site Configuration

First, open up your Nginx site config file (usually located at /etc/nginx/sites-available/your-domain.conf or similar). Look for the server block that handles your domain.

Step 2: Add the Rewrite Rule

Add one of these rules inside your server block (depending on whether you want an internal rewrite or a visible redirect):

Option 1: Internal Rewrite (URL stays the same for users)

This rule will silently route the /blog/?s=... request to the root search endpoint without changing the URL in the user's browser:

# Route /blog/ search requests to root WordPress search
rewrite ^/blog/?s=(.*)$ /?s=$1 last;

Option 2: 301 Permanent Redirect (URL changes to root)

If you want to permanently redirect users to the root search URL (good for SEO), use this instead:

# Redirect /blog/ search requests to root WordPress search (301 permanent)
rewrite ^/blog/?s=(.*)$ /?s=$1 permanent;

What do these parts mean?

  • ^/blog/?s=(.*)$: Matches URLs starting with /blog/ (the ? makes the trailing slash optional) followed by the search parameter s=. The (.*) captures the actual search term.
  • /s=$1: Rewrites/redirects to the root search URL, where $1 inserts the captured search term.
  • last: Tells Nginx to stop processing rules here and re-match the new URL against the config.
  • permanent: Sends a 301 redirect response, which tells browsers and search engines to update their links.

Step 3: Verify WordPress Settings

Double-check your wp-config.php file to ensure your site URLs are set correctly (this helps WordPress generate proper links):

define('WP_SITEURL', 'http://example.com');
define('WP_HOME', 'http://example.com');

Step 4: Test and Reload Nginx

Always test your Nginx config for syntax errors before reloading:

sudo nginx -t

If you see test is successful, restart Nginx to apply the changes:

sudo systemctl restart nginx

That should resolve the 404 error for your WordPress search requests from the /blog/ path!

内容的提问来源于stack exchange,提问作者sreelakshmi

火山引擎 最新活动