.htaccess将_转换为 - 来自所有网址

时间:2016-05-20 08:14:58

标签: regex apache .htaccess mod-rewrite

原始网址: [ { "children": [ { "children": [ { "text": "something" } ] } ] } ]

通缉网址 /icon/line-vector-icons-1-2/chatting_bubble-_talk-_comments-_chat-_online_conversation-62

/icon/line-vector-icons-1-2/chatting-bubble-talk-comments-chat-online-conversation-62”和“_”或“-_”应该像_- 如果我的问题已在其他地方得到解答,请告诉我,请随时编辑并更正我的问题。

这个问题有点像https://jsfiddle.net/75nrmL1o/问题,但它不符合我的要求,或者我做错了,这是我正在使用的.htaccess。如果出现问题,请告诉我。

-

1 个答案:

答案 0 :(得分:1)

您可以使用此递归规则(请参阅我的内联注释):

RewriteEngine On
RewriteBase /icon/

# keep replacing underscores to hyphen until there is no _ (use internal rewrite)
RewriteRule ^(.*?)(?:_-?|-_)(.*)$ $1-$2 [E=USCORE:1,DPI]

# when there is no underscore make an external redirection
RewriteCond %{ENV:USCORE} =1
RewriteRule ^([^_]+)$ $1 [NE,R=302,L]

RewriteCond $1 !^(index.php|resources|robots.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]