您可以使用您的/.htaccess
文件中的以下内容:
RewriteEngine On
# Replace underscores with hyphens, set the environment variable,
# and restart the rewriting process. This essentially loops
# until all underscores have been converted to hyphens.
RewriteRule ^([^_]*)_(.*)$ $1-$2 [E=underscore:yes,N]
# Then, once that is done, check if the underscore variable has
# been set and, if so, redirect to the new URI. This process ensures
# that the URI is rewritten in a loop *internally* so as to avoid
# multiple browser redirects.
RewriteCond %{ENV:underscore} yes
RewriteRule (.*) /$1 [R=302,L]
然后在后面加上您的规则:
RewriteRule ^music-(.+)-(\d+).html$ /artiste.php?g=$1&page=$2 [NC,L]
如果这是为你工作,你想使浏览器和搜索引擎缓存的重定向,将302
更改为301
。
注:在你RewriteRule
我已经改变.*
到.+
所以它只匹配一个或多个字符,而不是零个或多个字符。此外,我已将[0-9_]+
更改为\d+
,这是不包含下划线的等效速记,无论如何都将其转换为连字符。如果要在最后一个捕获组中包含连字符,请将(\d+)
更改为([\d-]+)
。
问题在于浏览器必须多次重定向,直到所有下划线消失。最好在内部进行转换。 –