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

使用.htaccess重定向:仅移除/blog/wp-content路径的blog目录

Fixing the .htaccess Redirect for /blog/wp-content Paths

I’ve dealt with this exact scenario before—let’s get your redirect working properly so only the /blog/wp-content/ paths lose the blog prefix, while keeping your actual blog content at /blog/ fully intact.

The Correct Rewrite Rule

Add this to your root .htaccess file (make sure it goes before any existing WordPress rewrite rules if you’re running WP):

RewriteEngine On
RewriteRule ^blog/wp-content/(.*)$ /wp-content/$1 [R=301,L]

Breakdown of How This Works:

  • RewriteEngine On: Makes sure mod_rewrite is enabled (you might already have this line in your file if you use WordPress).
  • ^blog/wp-content/(.*)$: The regex pattern that targets any URL starting with blog/wp-content/. The (.*) captures everything after that path—like 2007/06/DSC_1690.jpg in your example.
  • /wp-content/$1: The destination URL—$1 drops in the content we captured earlier, turning your example into /wp-content/2007/06/DSC_1690.jpg.
  • [R=301,L]: R=301 sets a permanent redirect (great for preserving SEO value), and L tells Apache this is the last rule to process for matching requests (avoids conflicts with other rules further down the file).

Why Your Earlier Rule Might Have Failed

If your previous RewriteRule ^blog... didn’t work, it’s probably one of these common issues:

  • You didn’t include the (.*) capture group—without that, Apache has no way to carry over the rest of the file path to the target URL.
  • The rule was placed after WordPress’s default rewrite block (# BEGIN WordPress), which would have intercepted the request first. Always put custom redirects before this block.
  • You forgot to enable RewriteEngine On (an easy oversight!).

Quick Testing Tip

Before locking in a 301 redirect (which gets cached by browsers), use R=302 temporarily to verify everything works:

RewriteRule ^blog/wp-content/(.*)$ /wp-content/$1 [R=302,L]

Once you confirm the redirect behaves as expected, switch back to R=301.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 11:18:52