某些 URL 上有尾随斜杠,但其他 URL 上没有 - 好的做法吗?我应该重定向吗?
在发布之前,我一直在研究标准化所有网站的 URL(出于显而易见的原因),并且一直在对这个主题进行大量研究。我得出以下结论:
- 尾部斜杠并不重要,只要同一页面没有多个 URL。
如果目录/集合(即帖子/或文章/)有尾部斜杠,而“文件”或特定项目(例如文章或博客文章)没有尾部斜杠,那么它看起来更好,并且对很多人来说更有意义t(即 posts/2012-02-05-Stackoverflow-rules 或articles/12-Proper-URLs)。
现在我显然愿意就这个主题进行辩论/其他意见,但现在我倾向于这种 URL 方法......话虽如此,强制执行此标准的最佳方法是什么?我的系统基于模块,我不知道我将拥有多少个模块,而且我真的不想为我(或其他人)添加的每个新模块使用新的 mod_rewrite 规则编辑 .htaccess 文件 - 这只会变得混乱。
在 PHP 中,有没有一些简单、可靠的方法可以在没有 htaccess 的情况下做到这一点?我需要吗?所有 URL 都将由一个类生成,该类将强化这些标准,这是一个合适的 MVC 框架 - 因此为不符合上述标准的页面添加一些代码不会那么困难 - 但是我只是不确定最好的方法是什么。
澄清
编辑为了澄清,我正在尝试弄清楚如何使某些链接具有尾部斜杠,而某些链接没有< /strong> 有尾部斜杠*,如果有人链接到错误的 URL(需要有尾部斜杠的 URL 上没有尾部斜杠,反之亦然),它会将用户重定向到正确的 URL。
*示例为
- www.example.com/articles/
- www.example.com/blog/
- www.example.com/forums/
对于需要尾部斜杠的页面,以及
- www.example.com/articles/21-Article-名称
- www.example.com/blog/2012/02/06/Blog-Post
- www.example.com/forums/post/29531/Post-Title
适用于不应有尾部斜杠的页面。基本上“目录”有斜杠,而“文件”没有。强调文本
那么基本上,实现此目的的最佳方法是什么?显然所有内部链接都会满足此要求标准,但我无法控制其他网站 - 并且它们带有或不带有尾部斜杠的链接可能会损害我的搜索引擎优化。与此相关的是,您对这种 URL 风格有何看法?
I've been looking into standardizing all of my sites URLs before I launch (For obvious reasons), and have been doing a lot of research on the subject. I've come to the following conclusions:
It does not matter if you use a forced trailing slash or forced no trailing slash, as long as you don't have multiple URLs for the same page.
It looks better, and makes more sense for a lot of people if directories/collections (I.e. posts/ or articles/) have a trailing slash, while "files" or specific items (An article or blog post for instance) don't (I.e. posts/2012-02-05-Stackoverflow-rules or articles/12-Proper-URLs).
Now I'm obviously open to debate / other opinions on this subject, but right now I'm leaning towards this method of URL... With that said, what would be the best way to force this standard? My system is based on modules, I have no idea how many modules I will have and I would really rather not edit the .htaccess file with a new mod_rewrite rule for every new module I (Or someone else) adds - that just gets messy.
Is there some easy, sure-fire way to do this without htaccess, in PHP? Do I even need to? All of the URLs are going to be generated by a single class which will reinforce those standards, and this is a proper MVC framework - so adding some code for pages that don't match the standards above wouldn't be all that difficult - but I'm just not sure what the best way to do it would be.
Clarification
Edit To clarify, I'm trying to figure out how to make some links have trailing slashes, and some links not have trailing slashes*, and if someone links to the wrong URL (No trailing slash on a URL that needs to have one, or vice versa) it will redirect the user to the proper url.
*An example would be
- www.example.com/articles/
- www.example.com/blog/
- www.example.com/forums/
For pages that need a trailing slash, and
- www.example.com/articles/21-Article-Name
- www.example.com/blog/2012/02/06/Blog-Post
- www.example.com/forums/post/29531/Post-Title
For pages that shouldn't have a trailing slash. Basically "directories" have slashes and "files" don't.emphasized text
So basically, what would be the best way to accomplish this? Obviously all internal links will meet this standard, but I am unable to control other sites - and them linking with or without a trailing slash may hurt my SEO. And on a related note, what is your opinion on this style of URL?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
无论您是否拥有它们,主要是为了您的方便问题。您应该保持一致并确保每页只有一个“真实”URL。至于重定向,我建议不要仅仅因为没有或有尾部斜杠而进行重定向。在这些情况下,重定向只是开销。
使用规范元标记向搜索引擎指示“真实”URL 是什么。我通常创建一个集中函数,可以根据某些规则检查当前 URL 并生成“真实”规范 URL。如果您的集中式函数变得太大或有太多规则,则您的 URL 结构可能太复杂。
Whether you have them or not is mainly a matter of convenience for you. You should be consistent and make sure their is only one "true" URL per page. As far redirects, I would recommend not redirecting just because there isn't or is a trailing slash. The redirect is just overhead in those cases.
Use the canonical meta tag to indicate to search engines what the "true" URL is. I usually create a centralize function that can check the current URL against some rules an generate the "true" canonical URL. If your centralized function is getting too large or has too many rules, your URL structure is probably too complicated.
我们最近遇到了同样的问题。我们决定在除了可能有 ?query_data 的页面之外的所有内容之后使用 / 。它使我们的检测分配更加简单。只需检查末尾是否存在尾部斜杠,除非?我们使用 PHP 来做到这一点。我确信也有一种 .htaccess 方法。它将是其中之一,无论最适合您的场景。您可能还想查看规范标签以及正确的 301 重定向(http://en. wikipedia.org/wiki/HTTP_301)这也将使您的搜索引擎记录更加清晰。
We recently had the same issue. We decided to go with / after everything except pages where there might be ?query_data. It made our detection allot simpler. Just check that a trailing slash exists at the end unless ? We used PHP to do this. Im sure there is an .htaccess approach too. Its going to be one of those, whatever works best for your scenario. You might want to take a look into canonical tags also, as well as proper 301 redirects ( http://en.wikipedia.org/wiki/HTTP_301 ) this will keep your search engine records clearer too.