简体   繁体   English

301将文件路径的正则表达式重定向到上载目录

[英]301 redirect regex for file paths to uploads directory

I am moving a website to wordpress and the site has hundreds of files available for download. 我正在将网站移至wordpress,该网站有数百个文件可供下载。 The current file path may have a number of subfolders before the name of the file. 当前文件路径在文件名之前可能包含多个子文件夹。 Below are some examples of how a url may look. 以下是网址外观的一些示例。

"http://www.example.com/uploads/folder/subfolder/anothersubfolder/file.pdf"

"http://www.example.com/uploads/folder/file.pdf"

  "http://www.example.com/uploads/folder/subfolder/anothersubfolder/anothersubfolder/file.doc"

I want to create a 301 redirect to handle any requests for these files and send them to the new url which would be 我想创建一个301重定向来处理对这些文件的任何请求,并将它们发送到新的url,

"http://www.example.com/wp-content/uploads/folder/subfolder"

I'm keeping the sub-folders the same in the uploads folder, so really I need a regex for pointing anything that goes to 我在上载文件夹中将子文件夹保持相同,因此,实际上我需要一个正则表达式来指向要

"http://www.example.com/uploads/"

and point it to 并指向

"http://www.example.com/wp-content/uploads/"

Update 更新资料

My current .htaccess file looks like 我当前的.htaccess文件看起来像

# 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

I've tried adding the answer from anubhava within the <IfModule mod_rewrite.c> block but I think there is an issue with the RewriteBase / from wordpress and me trying to add RewriteBase /uploads/ . 我尝试在<IfModule mod_rewrite.c>块中添加来自anubhava的答案,但我认为RewriteBase /从wordpress中出现问题,我尝试添加RewriteBase /uploads/ How can I have a rewrite condition for the /uploads directory? 我如何对/ uploads目录具有重写条件?

You can use this redirect rule in DocumentRoot/.htaccess : 您可以在DocumentRoot/.htaccess使用此重定向规则:

# BEGIN WordPress
<IfModule mod_rewrite.c>

RewriteEngine On
RewriteBase /

RewriteRule ^uploads(.*)$ /wp-content/uploads/$1 [L,NE,R=301]

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

</IfModule>    
# END WordPress

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM