简体   繁体   English

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

[英]batch 301 Redirect from Dynamic URLs to Static Pages with htaccess

An obsolete Wordpress image plugin has left me with over 4000 404 errors. 一个过时的Wordpress图像插件给我留下了4000 404个错误。

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: 为了解决该问题,我想批处理301重定向数以千计的旧附件链接URL,以指向原始源图像文件:

The old URLs that give a 404 error look like this: 出现404错误的旧网址如下所示:

http://www.hongkonghustle.com/?pagename=album&?pp_album=main&pp_cat=default&pp_image=zombie_boy_tattoo_lady_gaga_rick_genest.jpg 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: 我想将它们每个都重定向到文件实际存在的wp-content / photos目录:

http://www.hongkonghustle.com/wp-content/photos/zombie_boy_tattoo_lady_gaga_rick_genest.jpg 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. 从我的阅读中,我相信我应该更改我的htaccess文件并使用{QUERY_STRING}添加RewriteCond,但我不知道具体如何。

Also, where should I put the changes relative to my current htaccess? 另外,相对于我当前的htaccess,我应该将更改放在哪里?

My current htaccess file includes the following: 我当前的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 

Thanks again Anubhava! 再次感谢Anubhava! You're getting very close! 你已经很近了!

Now it goes to the /wp-content/photos/ directory, but the URL is still incorrect: 现在转到/ wp-content / photos /目录,但是URL仍然不正确:

hongkonghustle.com/wp-content/photos/&/?pagename=album&%253fpp_album=main&pp_cat=default&pp_image=zombie_boy_tattoo_lady_gaga_rick_genest.jpg 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: 我们需要的最终URL为:

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

So, hongkonghustle.com/wp-content/photos/IMAGE_FILENAME.JPG 因此,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! 真诚的感谢!

I ended up doing a 301 redirect like this in htaccess: 我最终在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

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  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM