批处理301使用htaccess从动态URL重定向到静态页面


batch 301 Redirect from Dynamic URLs to Static Pages with htaccess

一个过时的Wordpress图像插件给我留下了4000多个404错误。

为了解决这个问题,我想批量301重定向数千个旧的附件链接URL,指向原始的源图像文件:

给出404错误的旧URL如下所示:

http://www.hongkonghustle.com/?pagename=album&?pp_album=main&pp_cat=默认&pp_image=zombie_boy_attoo_lady_gaga_rick_generst.jpg

我想将它们重定向到文件实际存在的wp-content/photos目录:

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

根据我所读到的内容,我认为我应该更改我的htaccess文件,并使用{QUERY_STRING}添加RewriteCond,但我不知道具体如何。

此外,相对于我当前的htaccess,我应该把更改放在哪里?

我当前的htaccess文件包括以下内容:

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 

再次感谢阿努巴瓦!你越来越近了!

现在它转到/wp-content/photes/目录,但URL仍然不正确:

hongkonghustle.com/wp-content/photos/&/?pagename=相册&%253fpp_album=主&pp_cat=默认&pp_image=zombie_boy_attoo_lady_gaga_rick_generst.jpg

我们需要最终的URL为:

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

因此,hongkonghustle.com/wp-content/photos/IMAGE_FILENAME.JPG

我想它几乎解决了!如果你对如何实现这一点有任何想法,请向我更新。衷心感谢!

我最终在htaccess中做了一个301重定向,如下所示:

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

将代码更改为:

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