I’ve tried, I really have…I give in. WordPress and htaccess…oh the pain.
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.
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 /GraphicsFiles/(.*.jpg) /wp-content/uploads/$1
RedirectMatch 301 /GraphicsFiles/(.*.png) /wp-content/uploads/$1
RedirectMatch 301 /GraphicsFiles/(.*.gif) /wp-content/uploads/$1
I’ve been working off:
- 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 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.
Pete
Does it work if you change your section to look like this?
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.