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

时间:2013-10-12 14:42:44

标签: php wordpress .htaccess mod-rewrite redirect

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

要解决此问题,我想批量301重定向数千个旧附件链接网址以指向原始源图像文件:

出现404错误的旧网址如下所示:

http://www.hongkonghustle.com/?pagename=album&?pp_album=main&pp_cat=default&pp_image=zombie_boy_tattoo_lady_gaga_rick_genest.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 

再次感谢Anubhava!你越来越近了!

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

hongkonghustle.com/wp-content/photos/& /页面名=唱片&安培;%253fpp_album =主&安培; pp_cat =默认&安培; pp_image = zombie_boy_tattoo_lady_gaga_rick_genest.jpg

我们需要最终的网址:

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

所以,hongkonghustle.com/wp-content/photos/IMAGE_FILENAME.JPG

我认为它几乎已经解决了!如果您对如何完成此任务有任何想法,请更新我。真诚的谢谢!

2 个答案:

答案 0 :(得分:1)

我最终在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

答案 1 :(得分:0)

将您的代码更改为:

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