您可以使用您的/.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-]+)
。
問題在於瀏覽器必須多次重定向,直到所有下劃線消失。最好在內部進行轉換。 –