.htaccess将下划线重写为hypens、strip-extension和&;删除尾部下划线


.htaccess rewrite underscores to hypens, strip extension, & remove trailing underscores

我正在将我的博客从可移动类型转换为wordpress。有几千个条目,谢天谢地,我能够保持(几乎)相同的永久链接。

我知道有了一些聪明的.htaccess魔法,我应该能够重定向我的URL,但我一辈子都搞不清声明应该是什么。我有20000多个条目,所以在单个页面上重定向并不理想。

以下是我需要做的:

我有一堆这样的网址:

http://example.com/posttype/2013/05/04/story_name_and_such_.php

我需要:

if(extension is .php) {
  1. Convert underscores to hypens in the filename.
  2. Remove the file extension.
  3. Remove trailing hypens.
}

以上结果如下:

http://example.com/posttype/2013/05/04/story-name-and-such

有人做过类似的事吗?我真的需要帮助!

在文档根目录中的htaccess文件中这样做怎么样:

RewriteEngine On
RewriteRule ^(.+)_(.+)'.php /$1-$2.php [L,R=301]
RewriteRule ^(.+)_'.php /$1.php [L,R=301]
RewriteRule ^(.+)'.php /$1 [L,R=301]