大网数据广告活动
网站运营与故障问答

How can I use .htaccess to make old link retrieve content from new location?

· 52CE 我爱测

One of my sites had a WordPress site in the subdirectory (root)/blog/ . Using rules in .htaccess , the site was called up without the /blog/ in the URL. I recently changed it by removing the /blog/ directory, moving all the WP co…

问题原文

原帖链接 · inspirednz · CC BY-SA 3.0

One of my sites had a WordPress site in the subdirectory (root)/blog/.

Using rules in .htaccess, the site was called up without the /blog/ in the URL.

I recently changed it by removing the /blog/ directory, moving all the WP content into the root.

This particular blog auto-feeds all articles to a Facebook page. Each post on FB includes an the Featured Image from the blog entry. Since changing the directory structure, none of the images from the historical posts are showing up in Facebook.

What is the correct way to use .htaccess to make

mysite.com/blog/wp-content/uploads/*

call up the respective file from

mysite.com/wp-content/uploads/*

I have tried various Rewrite rules, but the ones I tried seemed to always redirect the URL to the new one, whereas I want the old URL to directly call up the file being requested, only from the files new location (in the way an Alias would work).

The existing .htaccess content

As requested, here is existing .htaccess code

# BEGIN WordPress
<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

UPDATE: Re answer I posted to this question

I worked out how to do this with a symlink. I would still like to know how to do it correctly with the .htaccess file (using rewrite rules, etc.), because setting up the symlink required SSH access, and I usually don't have SSH access to my sites.

已采纳解答

原帖链接 · MrWhite · CC BY-SA 3.0

Facebook would have been using the image URL as specified in the open graph tags. Since (by the sounds of it) the URL structure wasn't actually changed in WP (only in .htaccess) then this would have contained the /blog subdirectory in the URL. But then presumably you had to make an exception in .htaccess for the /blog/wp-content/uploads/ directory, or was FB following the redirect? Anyway...

To internally rewrite requests for /blog/wp-content/uploads/... to /wp-content/uploads/... you can try adding the following immediately after the RewriteBase directive (ie. before the main WP directives):

RewriteRule ^blog(/wp-content/uploads/.+) $1 [L]

ie. To remove the /blog prefix.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2016-01-20)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。