当前位置: 动力学知识库 > 问答 > 编程问答 >

apache - Trouble with advanced .htaccess redirect

问题描述:

I'm migrating a custom coded blog over to Wordpress, and have to set up a redirect that will handle all of the blog posts.

I need to redirect from this:

/oldblogdirectory/Old_Blog_Posts_Looked_Like_This.htm

to:

/newblogdirectory/new-blog-posts-look-like-this/

Any ideas on the regex for a redirect like this?

网友答案:

Gumbo's approach is certainly the way to do it. I made two test directories:

oldblogdir/archives/blog_posts_look_like_this.htm
newblogdir/archives/blog-posts-look-like-this

And the following RewriteRules redirect successfully. They are only slightly changed to Gumbo's proposal:

RewriteEngine on
RewriteBase /

RewriteRule ^(oldblogdir/archives/[^_]*)_(.*) $1-$2 [N]
RewriteRule ^oldblogdir/archives/(.*?)\.htm$ newblogdir/archives/$1 [R,NC,L]

Note that the [N] causes the .htaccess file to be re-evaluated until the RegEx no longer matches. Therefore you should put it at the very top of the file.

网友答案:

Try this mod_rewrite rules:

RewriteEngine on
RewriteRule ^(oldblogdirectory/[^_]*)_([^_]*)_(.*) /$1-$2-$3 [N]
RewriteRule ^(oldblogdirectory/[^_]*)_(.*) /$1-$2
RewriteRule ^oldblogdirectory/(.+)\.htm$ /newdirectory/$1/ [L,R=301]

But for the uppercase to lowercase conversion you’ll either need a mapping like the internal tolower function or you use PHP for both.

分享给朋友:
您可能感兴趣的文章:
随机阅读: