Php 下划线到连字符动态url

Php 下划线到连字符动态url,php,apache,.htaccess,mod-rewrite,Php,Apache,.htaccess,Mod Rewrite,我有以下现有的.htaccess RewriteEngine on RewriteCond %{HTTP_HOST} !^www\.example\.com RewriteRule (.*) http://www.example.com/$1 [R=301,L] RewriteRule home index.php [L] RewriteRule ^([^/]*)/$ /index.php?TopicID=$1 [L] RewriteRule ^([^/]*)/([^/]*)/$ /index

我有以下现有的.htaccess

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L] 
RewriteRule home index.php [L]
RewriteRule ^([^/]*)/$ /index.php?TopicID=$1 [L]
RewriteRule ^([^/]*)/([^/]*)/$ /index.php?RollID=$1&FrameID=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]
RedirectMatch 301 /cache(.*) //$1
ErrorDocument 404 http://www.example.com/error.php
我有许多动态生成的URL,这些URL被重写为

www.example.com/abc_def_ghi/或 www.example.com/abc_def_ghi/abc_def_ghi/

我想以这个结束

www.example.com/abc-def-ghi/或 www.example.com/abc-def-ghi/abc-def-ghi/

我已尝试将此添加到.htaccess的末尾

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !.(html|php)($|?) [NC]
RewriteRule ^([^_]*)_+(.*)$ $1-$2 [E=underscores:Yes,N]
RewriteCond %{ENV:underscores} ^Yes$
RewriteRule (.*) http://%{HTTP_HOST}/$1 [R=301,L]
但我得到了一个500的错误。
有人能帮忙吗?

像这样重新安排规则:

ErrorDocument 404 http://www.example.com/error.php
RewriteEngine on

RewriteRule ^cache(.*) /$1 [L,R=301]

RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L] 

# if there is only one underscore then repalce it by - and redirect
RewriteRule ^([^_]*)_([^_]*)$ /$1-$2 [L,R=302]

# if there are more than one underscores then "repeatedly" repalce it by - and set env var
RewriteRule ^([^_]*)_([^_]*)_(.*) $1-$2-$3 [E=USCORE:1]

# if USCORE env var is set and there is no underscore left then redirect
RewriteCond %{ENV:REDIRECT_USCORE} =1
RewriteRule ^([^_]+)$ /$1 [L,R=302]

RewriteRule home index.php [L]

RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteRule ^([^/]+)/?$ /index.php?TopicID=$1 [L,QSA]

RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?RollID=$1&FrameID=$2 [L,QSA]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]

如果它是使用下划线动态生成的,只需将其更改为在代码中使用连字符即可。解决实际问题。