对于移动PDF文件htaccess的规则?(htaccess rule for moved pdf

2019-09-20 16:11发布

我尝试过,我真的......我放弃。Wordpress和htaccess的...哦痛苦。

旧的静态网站有PDF,DOC,XLS和ZIP文件的所有根。 我已经加载的所有文件作为媒体项目,使他们都生活在/可湿性粉剂内容/上传。

我只是想捕捉任何404是.pdf文件重定向到文件夹上传。 应该很容易,但我显然不是做正确的事情。 htaccess具有SOOOO许多标志和设置的东西多来发生冲突或者我只是没有“得到它”。

RewriteEngine On

# Catch incorrect case (must be a better way of doing this?)
RewriteCond %{REQUEST_FILENAME} !-f  
RewriteRule (.*)\.JPG$ $1.jpg [L,R=301] 

RewriteCond %{REQUEST_FILENAME} !-f  
RewriteRule (.*)\.PNG$ $1.png [L,R=301] 

RewriteCond %{REQUEST_FILENAME} !-f  
RewriteRule (.*)\.GIF$ $1.gif [L,R=301] 

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

# Start of my woes...what am I doing wrong?
RewriteCond %{REQUEST_FILENAME} .*\.pdf [NC]
#RewriteCond %{REQUEST_FILENAME} !\/wp-content\/uploads\/
RewriteRule ([^/]*\.pdf) /wp-content/uploads/$1 [NC,L,R=301] 

RewriteCond %{REQUEST_FILENAME} !-f  
RewriteRule ([^\/]*.zip) /wp-content/uploads/$1 [L,R=301] 

RewriteCond %{REQUEST_FILENAME} !-f  
RewriteRule ([^\/]*.xls) /wp-content/uploads/$1 [L,R=301] 

# For bonus points, is this the correct way to do this? Catch old reference to an old folder and move them on?
RedirectMatch 301 /Graphic\sFiles/(.*\.jpg) /wp-content/uploads/$1
RedirectMatch 301 /Graphic\sFiles/(.*\.png) /wp-content/uploads/$1
RedirectMatch 301 /Graphic\sFiles/(.*\.gif) /wp-content/uploads/$1

我已经工作过:

  • http://httpd.apache.org/docs/current/rewrite/flags.html
  • http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule
  • http://httpd.apache.org/docs/current/rewrite/remapping.html#fallback-resource

SOOOO许多规则采取了我认为是一个简单的工作。 我没有htaccess的大师也绝不会,我有足够的尊重那些虽然!

感谢提前女士们,先生们任何指针。

皮特

Answer 1:

请问,如果你改变你的部分看起来像这样工作?

RewriteCond %{REQUEST_FILENAME} !-f  
RewriteCond %{REQUEST_URI} !^/wp-content/uploads/
RewriteRule ([^/]*\.(pdf|zip|xls))$ /wp-content/uploads/$1 [NC,L,R=301] 

第一个条件检查该请求是针对不存在的文件,第二个是确保我们尚未重定向到上传目录(如果它不存在任),以及规则对比赛无论是PDF,zip或XLS。



文章来源: htaccess rule for moved pdf files?