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

和自甴很熟 提交于 2019-12-24 00:09:27

问题


I'm converting my blog from movable type to wordpress.There are several thousand entries, and thankfully I am able to keep (almost) the same permalinks.

I know that with some clever .htaccess wizardry, I should be able to redirect my URLs, but I can't for the life of me figure out what the statement(s) should be.I have over 20,000 entries, so redirects on individual pages is not ideal.

Here's what I need to do:

I have a bunch of URLs like this:

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

I need to :

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

The above would end up as such:

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

Anyone done something similar to this? I could really use the help on this one!


回答1:


How about something like this, in the htaccess file in your document root:

RewriteEngine On

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


来源:https://stackoverflow.com/questions/18564065/htaccess-rewrite-underscores-to-hypens-strip-extension-remove-trailing-unde

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!