我试过了,我真的……我屈服了。Wordpress 和 htaccess ……哦,痛苦。
旧静态站点的根目录中包含 pdf、doc、xls 和 zip 文件。我已将所有文件作为媒体项加载,因此它们都位于 /wp-content/uploads 中。
我只是想捕获任何 .pdf 文件的 404 并将其重定向到上传文件夹。应该很容易,但我显然没有做对。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/mod/mod_rewrite.html#rewriterule
- http://httpd.apache.org/docs/current/rewrite/remapping.html#fallback-resource
对于我认为是一项简单的工作,需要接受很多规则。我不是 htaccess 大师,也永远不会是,我非常尊重那些虽然是的人!
感谢女士们和先生们提前提供的任何指示。
皮特