【问题标题】:User-friendly links behavior without mod_rewrite没有 mod_rewrite 的用户友好的链接行为
【发布时间】:2021-04-03 16:45:07
【问题描述】:

我想创建用户友好的链接(例如:http://www.website.com/post-id/this-is-post-title),但是当用户点击“http://www.website.com/post-id/changed-post-title-by-user”地址时应该被替换 - 立即且无需重定向 - 到“http://www.website.com/post-id/post-title”,其中“post-title”是 ID 等于“post-”的帖子的标题id" 就像 StackOverFlow 链接的行为一样。我知道推送历史状态 API:

history.pushState(state, title [, url])

但此方法需要启用 Javascript,并且某些网站(如 StackOverFlow 方法)在禁用 JS 的情况下工作。 我像这样处理“index.php”中的链接:

$requestURI = explode('/', $_SERVER['REQUEST_URI']);
$command = array_values($requestURI);

if(condition)
{
  include('page.php');
}

【问题讨论】:

  • 本质上,你的意思是你想要重定向而不使用重定向。祝你好运......更改浏览器中显示的 url 的唯一方法是直接使用推送状态重定向或操作它。 FWIW,重定向是友好网址的工作方式。
  • @TimMorton 获取我的问题“stackoverflow.com/questions/65451745/…”的链接,您可以将最后一部分“user-friendly-links-behavior-without-mod-rewrite”更改为您想要的任何内容,链接将即使您禁用了推送状态所基于的javascript,也可以立即更改为当前链接而无需重定向。
  • 我一直试图弄清楚你在说什么机制:什么改变了链接,什么事件?你是说当你输入 url a 时,它会在你按 enter 后显示为 url b?
  • @TimMorton 此链接“stackoverflow.com/questions/65451745/whatever”将显示正确的帖子,但地址栏将显示此链接“stackoverflow.com/questions/65451745/…

标签: javascript php html apache


【解决方案1】:

您观察到的行为是服务器重定向。当你输入

https://stackoverflow.com/questions/65451745/whatever

服务器收到该请求并发出301 状态代码(永久移动重定向)

Request URL: https://stackoverflow.com/questions/65451745/whatever
Request Method: GET
Status Code: 301  (from disk cache)

重定向的位置是/questions/65451745/user-friendly-links-behavior-without-mod-rewrite,如下面的响应标头所示:

accept-ranges: bytes
cache-control: private
content-security-policy: upgrade-insecure-requests; frame-ancestors 'self' https://stackexchange.com
date: Sat, 26 Dec 2020 02:50:42 GMT
feature-policy: microphone 'none'; speaker 'none'
location: /questions/65451745/user-friendly-links-behavior-without-mod-rewrite
server: Microsoft-IIS/10.0
vary: Fastly-SSL
via: 1.1 varnish
x-account-id: 2441033
x-aspnet-duration-ms: 1
x-cache: MISS
x-cache-hits: 0
x-dns-prefetch-control: off
x-flags: AA
x-frame-options: SAMEORIGIN
x-is-crawler: 0
x-providence-cookie: 372ce5a1-ff98-0aae-7962-a68742f9dfee
x-request-guid: f5cc8c0f-2ac7-427c-8f1f-aa007566d70f
x-route-name: Questions/Show
x-served-by: cache-mdw17325-MDW
x-sql-count: 2
x-sql-duration-ms: 1
x-timer: S1608951042.245905,VS0,VE26

长话短说,如果您想“在飞行中”更改网址,重定向是唯一的方法。它不是在 php 或 html 中完成的;这是一个服务器功能。

【讨论】:

  • 很久没有在 apache 中设置了。我能做的最好的就是为你指明正确的方向。密钥是mod_rewrite
  • 即使使用 mod_rewrite ,服务器(Apache 或 IIS)如何重定向到具有正确帖子标题的链接(链接的最后部分)?需要数据库连接!
  • 一种可能的情况:所有通过 index.php 路由的请求都会解析 url(mod rewrite 可能会添加路径作为查询字符串),验证,如果无效,则查找链接最后一部分的值,并且问题 301 重定向到正确的地址。如果地址正确,它只会路由到正确的控制器、模型和视图。 (或者对于非 mvc,包括正确的脚本)
  • 我使用这种方法,正如您在我的问题中看到的,但没有 mod_rewrite,检查帖子标题是否正确是资源消耗过程,而且似乎很慢。而且我认为 StackOverFlow 和其他网站没有使用这种方法。
  • 正如您所指出的,他们必须访问数据库才能获得标题。一次数据库调用根本不是什么大问题。查找 url slugs 的概念。如果提供的 slug != 查找 slug,则重定向到查找的 slug。如果它们匹配,则加载匹配的控制器等。
猜你喜欢
  • 1970-01-01
  • 2011-01-04
  • 2013-08-10
  • 2012-02-25
  • 2017-06-28
  • 1970-01-01
  • 1970-01-01
  • 2012-02-24
  • 1970-01-01
相关资源
最近更新 更多