我应该使用什么字符来替换 url slug 中的非法字符

发布于 2024-07-21 08:44:40 字数 114 浏览 11 评论 0原文

我注意到各种系统使用不同的字符来替换网址中的非法字符。

是否有理由使用其中之一,或者我应该选择最适合我的一个

到目前为止我看到的选项包括: - _ + 并简单地删除所有非法字符。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

故笙诉离歌 2024-07-28 08:44:40

只需使用 - 代替空格并删除非法字符(就像这个网站一样)。

而且都是小写的。

Just use - for space and get rid of the illegal chars (like this site is).

Also it's all lower-case.

掩于岁月 2024-07-28 08:44:40

我个人会使用 _ 来替换非法字符,并使用 - 来替换空格。 另一种选择是简单地删除非法字符。

I would personally use _ to replace illegal characters and - for space. One other option would be to simply remove the illegal characters.

笨死的猪 2024-07-28 08:44:40

我的偏好是“-”,我使用一个非常简单的正则表达式来替换我不想要的所有内容。

[^a-zA-Z0-9\-]*

这将用破折号替换所有非字母数字字符和破折号字符。

My preference is "-" and I use a very simple RegEx to replace everything that I don't want.

[^a-zA-Z0-9\-]*

This will replace any non alpha numeric characters and dash characters with a dash.

辞别 2024-07-28 08:44:40

省略字符可能会产生非常奇怪的字符串。 真正奇怪的字符串对 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文