You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用.htaccess实现WordPress所有404页面重定向至首页(排除wp-admin及指定URL)

Fix 404 Redirect to Homepage (Excluding wp-admin & Specific URL)

Got it, let's get this sorted for your WordPress site. The main issue with your previous attempts was likely rule order or missing exclusion conditions—.htaccess processes rules top to bottom, so we need to explicitly skip the paths you don't want redirected before applying the global 404 rule.

Step-by-Step Solution

First, edit your site's root .htaccess file (you can access this via FTP, cPanel File Manager, or a plugin like File Manager for WordPress). Place the custom redirect rules before the default WordPress block (marked with # BEGIN WordPress), since WP's permalink rules need to run after our exclusions.

Here's the complete code to use:

# Custom 404 Redirect: Exclude wp-admin & specific URL, send others to homepage
RewriteEngine On

# Exclude wp-admin directory (and all its subpaths)
RewriteCond %{REQUEST_URI} !^/wp-admin/
# Exclude your specified URL - replace /your-specified-url/?$ with your actual path
# The ?$ matches both with/without a trailing slash
RewriteCond %{REQUEST_URI} !^/your-specified-url/?$

# Only apply redirect if the request is for a non-existent file/directory (i.e., 404)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Redirect to homepage with permanent 301 status (use R=302 for temporary redirects)
RewriteRule ^ / [R=301,L]

# BEGIN WordPress
# Leave the default WordPress rules intact below this line
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Key Details to Adjust

  • Replace the specified URL: Swap /your-specified-url/?$ with your actual path. For example, if you want to exclude /special-landing-page, change it to !^/special-landing-page/?$. If it's a directory like /docs/, use !^/docs/.
  • Redirect type: Use R=301 for permanent redirects (best for SEO if you don't plan to restore the 404 pages) or R=302 for temporary redirects.
  • Subdirectory installs: If your WordPress is in a subdirectory (e.g., /blog/), update RewriteBase / to RewriteBase /blog/ and change the redirect target to /blog/ in the RewriteRule.

Testing Tips

  1. Clear your browser cache before testing—301 redirects are cached aggressively.
  2. Test a random non-existent URL (e.g., /this-page-doesnt-exist) to confirm it redirects to the homepage.
  3. Test wp-admin and your specified URL to make sure they don't redirect (they should load normally or show their own 404 if applicable).

If you still run into issues, double-check that mod_rewrite is enabled on your server (most shared hosts have this enabled by default, but you can confirm with your host if needed).

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 21:13:10