N. King
N. King

Reputation: 31

batch 301 Redirect from Dynamic URLs to Static Pages with htaccess

An obsolete Wordpress image plugin has left me with over 4000 404 errors.

To solve the problem, I'd like to batch 301 redirect thousands of old attachment link URLs to point towards the original source image files:

The old URLs that give a 404 error look like this:

http://www.hongkonghustle.com/?pagename=album&?pp_album=main&pp_cat=default&pp_image=zombie_boy_tattoo_lady_gaga_rick_genest.jpg

I'd like to redirect them each to the wp-content/photos directory where the file actually exists:

http://www.hongkonghustle.com/wp-content/photos/zombie_boy_tattoo_lady_gaga_rick_genest.jpg

From what I've read, I believe I should alter my htaccess file and add a RewriteCond using the {QUERY_STRING} but I don't know exactly how.

Also, where should I put the changes relative to my current htaccess?

My current htaccess file includes the following:

Options +Indexes
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^hongkonghustle\.com
RewriteRule ^(.*)$ http://www.hongkonghustle.com/$1 [R=permanent,L]

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.hongkonghustle.com/$1/ [L,R=301]

RewriteEngine On  
RewriteBase / 
<Files wp-config.php>
Deny from all
</Files>

<Files wp-config.php>
Deny from all
</Files>
Options +Indexes
IndexOptions -FancyIndexing  

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress 

Thanks again Anubhava! You're getting very close!

Now it goes to the /wp-content/photos/ directory, but the URL is still incorrect:

hongkonghustle.com/wp-content/photos/&/?pagename=album&%253fpp_album=main&pp_cat=default&pp_image=zombie_boy_tattoo_lady_gaga_rick_genest.jpg

We need the final URL to be:

hongkonghustle.com/wp-content/photos/zombie_boy_tattoo_lady_gaga_rick_genest.jpg

So, hongkonghustle.com/wp-content/photos/IMAGE_FILENAME.JPG

I think it's almost solved! Please update me if you have any ideas on how to accomplish this. Sincere thanks!

Upvotes: 0

Views: 1284

Answers (2)

N. King
N. King

Reputation: 31

I ended up doing a 301 redirect like this in htaccess:

Options +Indexes
Options +FollowSymLinks
RewriteEngine on
RewriteCond  %{QUERY_STRING}  ^pagename=album&\??pp_album=main&pp_cat=default&pp_image=(.*)$
RewriteRule .* /wp-content/photos/%1? [L,R=301]

RewriteCond %{HTTP_HOST} ^hongkonghustle\.com
RewriteRule ^(.*)$ http://www.hongkonghustle.com/$1 [R=permanent,L]

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.hongkonghustle.com/$1/ [L,R=301]

RewriteEngine On  
RewriteBase / 
<Files wp-config.php>
Deny from all
</Files>

<Files wp-config.php>
Deny from all
</Files>
Options +Indexes
IndexOptions -FancyIndexing


# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

Upvotes: 1

anubhava
anubhava

Reputation: 785156

Change your code to this:

Options +Indexes +FollowSymLinks -MultiViews

RewriteEngine on
RewriteBase / 

RewriteCond %{QUERY_STRING} (^|&)pp_image=([^&]+) [NC]
RewriteRule ^$ /wp-content/photos/%1 [R=301,L]

RewriteCond %{HTTP_HOST} ^hongkonghustle\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^ %{REQUEST_URI}/ [L,R=301]

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

<Files wp-config.php>
Deny from all
</Files>

IndexOptions -FancyIndexing  

Upvotes: 0

Related Questions