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

时间:2022-09-11 10:23:31

I noticed that various systems use various characters as the replacent for illegal ones in urls.

我注意到各种系统使用各种字符作为url中非法的重放。

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.

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

4 个解决方案

#1


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

只需使用 - 为空间和摆脱非法的chars(像这个网站是)。

Also it's all lower-case.

它也都是小写的。

#2


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

我个人会用_来代替非法字符和 - 用于空间。另一种选择是简单地删除非法字符。

#3


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

我的偏好是“ - ”,我使用一个非常简单的RegEx来替换我不想要的一切。

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

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

这将用短划线替换任何非字母数字字符和短划线字符。

#4


Leaving out characters can make really strange strings. Really strange strings do not help for SEO.

离开字符可以制作非常奇怪的字符串。真奇怪的字符串对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)

“最漂亮”的解决方案是将非ascii字符音译为ascii等效字符。这可以使用Iconv完成(如果你在unix平台上)

You could also take a look at: How to handle diacritics (accents) when rewriting ‘pretty URLs’

您还可以看看:如何在重写“漂亮的URL”时处理变音符号(重音符号)

But that is a PHP-specific question

但这是一个特定于PHP的问题

Hope this helps

希望这可以帮助

#1


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

只需使用 - 为空间和摆脱非法的chars(像这个网站是)。

Also it's all lower-case.

它也都是小写的。

#2


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

我个人会用_来代替非法字符和 - 用于空间。另一种选择是简单地删除非法字符。

#3


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

我的偏好是“ - ”,我使用一个非常简单的RegEx来替换我不想要的一切。

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

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

这将用短划线替换任何非字母数字字符和短划线字符。

#4


Leaving out characters can make really strange strings. Really strange strings do not help for SEO.

离开字符可以制作非常奇怪的字符串。真奇怪的字符串对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)

“最漂亮”的解决方案是将非ascii字符音译为ascii等效字符。这可以使用Iconv完成(如果你在unix平台上)

You could also take a look at: How to handle diacritics (accents) when rewriting ‘pretty URLs’

您还可以看看:如何在重写“漂亮的URL”时处理变音符号(重音符号)

But that is a PHP-specific question

但这是一个特定于PHP的问题

Hope this helps

希望这可以帮助