2014-01-09 109 views
0

我将wordpress文件从根目录移至子目录。有一些文件上传到wp-content/uploads文件夹中。当直接访问这些文件时,我得到一个404错误。通过的.htaccess在.htaccess中重写子目录的url

我需要重写URL只为的wp-content /上传

我需要的是从 http://example.com/wp-content/uploads/2012/09/report.pdf 重定向到 http://example.com/wp_sub/wp-content/uploads/2012/09/report.pdf

这是我已经试过

的RewriteCond%{HTTP_HOST} ^(WWW)。?example.com $

重写规则^(。*)$ http://www.example.com/wp_sub/ $ 1 [R = 301, QSA,L]

这会为每个链接添加一个子目录wp_sub。 我需要为wp-content/uploads链接添加一个子目录“wp_sub”。

我错过了什么?

回答

2

让我们来看看

RewriteRule ^(.*)$ http://www.example.com/wp_sub/$1 

意味着什么:
你重写一切(^(.*)$)到http://www.example.com/wp_sub/$1
那么,你不想重写所有的东西,是吗? :)

因此,我们必须限制这种只影响wp-content文件夹:

RewriteRule ^(wp-content/.*)$ http://www.example.com/wp_sub/$1 [R=301,QSA,L] 
0

您可以使用代码:

RewriteRule ^wp-content/uploads/(.*)$ http://www.example.com/wp_sub/wp-content/uploads/$1 [R=301,L] 
1

将这个规则在/wp-content/uploads/.htaccess文件:

RewriteEngine On 
RewriteBase /wp-content/uploads/ 

RewriteRule ^(.*)$ /wp_sub/$1 [L,R]