使用.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 withblog/wp-content/. The(.*)captures everything after that path—like2007/06/DSC_1690.jpgin your example./wp-content/$1: The destination URL—$1drops in the content we captured earlier, turning your example into/wp-content/2007/06/DSC_1690.jpg.[R=301,L]:R=301sets a permanent redirect (great for preserving SEO value), andLtells 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
相关产品推荐
相关产品推荐

