URL 重写、SEO 和编码

发布于 2024-08-17 20:28:11 字数 490 浏览 3 评论 0原文

我发现这个 关于 URL 重写的文章最有用。 但这里有几个问题。

我想使用 URL(在重写之前,查询字符串中包含空格)

http://www.store.com/products.aspx?category=CD s-Dvd s 

首先,我是否应该出于任何原因用加号 (+) 替换空格?像这样:

http://www.store.com/products.aspx?category=CD+s-Dvd+s 

其次,我的母语是希腊语。我应该对参数进行编码吗?一般来说,对于 SEO 而言,启用 URL 编码的结果会有所不同吗?

I found this article regarding URL Rewriting most useful.
But here are a couple of questions.

I would love to use a URL (before rewriting, with spaces inside the query string)

http://www.store.com/products.aspx?category=CD s-Dvd s 

First of all, should I replace the spaces with the plus sign (+) for any reason? Like this:

http://www.store.com/products.aspx?category=CD+s-Dvd+s 

Secondly, my native language is Greek. Should I encode the parameters? Generally speaking, would the result with URL encoding on be different, regarding S.E.O.?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

愛上了 2024-08-24 20:28:11

实际上你应该用连字符替换空格。这实际上比使用下划线更有利于搜索引擎优化。

Actually you should replace spaces with hyphens. That actually is better for SEO than using an underscore.

五里雾 2024-08-24 20:28:11

如果值必须保持不变,那么您必须使用转义。 URL查询参数值中的空格可以编码为+%20。只要外部版本拼写正确,mod_rewrite 通常就会为您执行此操作。

在 URL 的外部版本中,只能使用 %20:

http://www.store.com/products/CD%20s-Dvd%20s
http://www.store.com/products.php?category=CD%20s-Dvd%20s

因为 URL 路径部分中的 + 字面意思是加号。

(您确定要在那里有一个空格吗?不带空格的“CD-DVD”似乎是一个更好的标题。)

从路径部分到参数获取任意字符串并非易事。除了转义问题之外,您还遇到 / 问题,它应该在路径部分中编码为 %2F 。但是,出于安全原因,Apache 默认情况下会阻止任何包含 %2F 的 URL。 (\ 在 Windows 下同样受到影响。)您可以使用 AllowEncodedSlashes 配置关闭此行为,但这意味着如果您想要便携,则不能使用“CD” /DVDs”作为类别名称。

出于这个原因,并且因为在 URL 中加载 %20 有点难看,所以字符串在放入 URL 之前通常会转换为“slugs”,其中所有有争议的 ASCII 字符都被转换为“slugs”。将导致可见的 %-转义符被替换为填充字符,例如连字符或下划线。这确实意味着您无法往返字符串,因此您需要在数据库中存储单独的标题和 slug,以便能够查找给定 slug 的正确实体,或者仅在 URL 中使用附加 ID (就像堆栈溢出一样)。

If the value must come through unaltered, then yes you must use escaping. In a URL query parameter value, a space may be encoded as + or %20. mod_rewrite will generally do this for you as long as the external version was suitably spelled.

In the external version of the URL, only %20 can be used:

http://www.store.com/products/CD%20s-Dvd%20s
http://www.store.com/products.php?category=CD%20s-Dvd%20s

because a + in a URL path part would literally mean a plus.

(Are you sure you want a space there? “CDs-DVDs” without the spaces would seem to be a better title.)

It is non-trivial to get arbitrary strings through from a path part to a parameter. Apart from the escaping issues, you've got problems with /, which should be encoded as %2F in a path part. However Apache will by default block any URL containing %2F for security reasons. (\ is similarly affected under Windows.) You can turn this behaviour off using the AllowEncodedSlashes config, but it means if you want to be portable you can't use “CDs/DVDs” as a category name.

For this reason, and because having a load of %20​s in your URL is a bit ugly, strings are usually turned into ‘slugs’ before being put in a URL, where all the contentious ASCII characters that would result in visible %-escapes are replaced with filler characters such as hyphen or underscore. This does mean you can't round-trip the string, so you need to store either a separate title and slug in the database to be able to look up the right entity for a given slug, or just use an additional ID in the URL (like Stack Overflow does).

东走西顾 2024-08-24 20:28:11

一般做法是用下划线替换空格,ala http://www.store.com/products.aspx?category=CD_s-Dvd_s

General practice is to replace spaces with underscores, ala http://www.store.com/products.aspx?category=CD_s-Dvd_s

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