我应该使用什么字符来替换 url slug 中的非法字符
我注意到各种系统使用不同的字符来替换网址中的非法字符。
是否有理由使用其中之一,或者我应该选择最适合我的一个
到目前为止我看到的选项包括: - _ + 并简单地删除所有非法字符。
I noticed that various systems use various characters as the replacent for illegal ones in urls.
Is there a reason to use one or the other or should I just pick the one that looks best to me
The options I have seen so far include: - _ + and simply removing all illegal characters.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只需使用 - 代替空格并删除非法字符(就像这个网站一样)。
而且都是小写的。
Just use - for space and get rid of the illegal chars (like this site is).
Also it's all lower-case.
我个人会使用 _ 来替换非法字符,并使用 - 来替换空格。 另一种选择是简单地删除非法字符。
I would personally use _ to replace illegal characters and - for space. One other option would be to simply remove the illegal characters.
我的偏好是“-”,我使用一个非常简单的正则表达式来替换我不想要的所有内容。
这将用破折号替换所有非字母数字字符和破折号字符。
My preference is "-" and I use a very simple RegEx to replace everything that I don't want.
This will replace any non alpha numeric characters and dash characters with a dash.
省略字符可能会产生非常奇怪的字符串。 真正奇怪的字符串对 SEO 没有帮助。
“最漂亮”的解决方案是将非 ASCII 字符音译为对应的 ASCII 字符。
这可以使用 Iconv 来完成(如果您是在unix平台上)
您还可以看一下: 如何在重写“漂亮的 URL”时处理变音符号(重音)
但这是 PHP 特定的问题
希望这会有所帮助
Leaving out characters can make really strange strings. Really strange strings do not help for SEO.
The 'prettiest' solution is to transliterate your non-ascii characters to their ascii-equivalent.
This can be done using Iconv (if you are on a unix platform)
You could also take a look at: How to handle diacritics (accents) when rewriting ‘pretty URLs’
But that is a PHP-specific question
Hope this helps