对于移动PDF文件htaccess的规则? [英] htaccess rule for moved pdf files?

查看:130
本文介绍了对于移动PDF文件htaccess的规则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过,我真的......我放弃。字preSS和htaccess的...哦疼痛。

I've tried, I really have...I give in. Wordpress and htaccess...oh the pain.

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

Old static site has pdf, doc, xls and zip files all in the root. I've loaded all the files as media items so they all live in /wp-content/uploads.

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

I simply want to catch any 404 which is a .pdf file and redirect it to the upload folder. Should be easy but I'm clearly not doing something right. htaccess has soooo many flags and settings that something much be clashing or I'm just not "getting it".

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的大师也绝不会,我有足够的尊重那些虽然!

Soooo many rules to take in for what I thought was one simple job. I'm no htaccess guru and never will be, I have plenty of respect for those that are though!

感谢提前女士和男士的指针。

Thanks for any pointers in advance ladies and gents.

皮特

推荐答案

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

Does it work if you change your section to look like this?

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相匹配。

First condition checks that the request is for a file that does not exist, the second is to make sure we've not already redirected to the uploads directory (in case it doesn't exist in there either), and the rule matches against either pdf, zip or xls.

这篇关于对于移动PDF文件htaccess的规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆