我已经尝试过,我真的已经...我屈服了。Wordpress 和 htaccess...哦,痛苦。
旧的静态站点的根目录中有 pdf、doc、xls 和 zip 文件。我已将所有文件作为媒体项加载,因此它们都位于 /wp-content/uploads 中。
我只是想捕获任何 404 .pdf 文件并将其重定向到上传文件夹。应该很容易,但我显然没有做正确的事情。 htaccess 有太多的标志和设置,有些东西会发生冲突,或者我只是不“明白”。
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/rewrite/flags.html
- http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule
- http://httpd.apache.org/docs/current/rewrite/remapping.html#fallback-resource http://httpd.apache.org/docs/current/rewrite/remapping.html#fallback-resource
对于我认为是一项简单的工作,需要遵守太多规则。我不是 htaccess 专家,也永远不会,但我对那些专家非常尊重!
感谢女士们先生们提前提出的任何指示。
Pete
如果您将您的部分更改为如下所示,它会起作用吗?
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。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)