谁能帮我把旧的动态 url 格式改成新的动态 url 格式?
准确地说,我需要将 url 中的下划线 _ 更改为连字符 - 但使用 301 重定向 在 htaccess 中重定向,例如:
- example.com/posts/22_keyword/33_this_is_the_example_of_old_url.html
我希望它是:
- example.com/posts/22-keyword/33-this-is-the-example-of-old-url.html
我知道如何在 php 中更改它,但我需要使其与从旧 url 格式到新格式的 301 重定向一起使用 在 htaccess 中,这样我就不会失去在 google 上的排名。
这是当前的 htaccess 信息:
RewriteRule ^posts/([0-9]+)([-_][^/]*)?/([0-9]+)([-_][^/]*)?/([0-9]+)([-_][^/]*)?\.html index.php?view=showad&adid=$7&cityid=$1 [QSA]
转换为:
- posts/3_keyword/6_keyword/235530_this_is_the_example_of_old_url.html
我不熟悉 mod rewrite,我想这对于了解它的人来说很容易,因为 所需要做的就是将 _ 更改为 -
最佳答案
您可以使用此递归规则将 _
转换为 -
:
RewriteEngine On
# using internal rewrites replace all _ by -
RewriteRule ^([^_]+)_(.+?\.html)$ $1-$2 [NC,DPI,E=underscores:1]
# one final redirect to change URL in browser
RewriteCond %{ENV:underscores} =1
RewriteRule ^([^_]+)$ /$1 [R=302,NE,L]
关于.htaccess - 使用 301 重定向将旧的动态 URL 更改为新格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36967019/