动态 url 并在页面 url 末尾添加标题
我正在制作一个内容是动态的页面,但到目前为止我只能使用变量获取我想要的页面,例如(以 stackoverflow 为例): https://stackoverflow.com/questions?q=7980734
但正如 stackoverflow 中所见,他们拥有它,因为
https://stackoverflow.com/questions/7980734/dynbamic-url-and-adding-a-the-title-to-the-end-of-the-page-url
我只是想知道他们是如何制作的(使用 /)而不创建个人每个问题的页面。
以及他们如何将标题添加到网址末尾而不创建新目录。 即,如果您输入,
https://stackoverflow.com/questions/7980734/
它仍然会带您进入问题,然后添加标题。
感谢您的帮助!
I am making a page where the content is dynamic, but up til now i have only been able to get the page i want with variables such as (using stackoverflow as a example): https://stackoverflow.com/questions?q=7980734
but as seen in stackoverflow they have it as
https://stackoverflow.com/questions/7980734/dynbamic-url-and-adding-a-the-title-to-the-end-of-the-page-url
i just want to know how they make like that (using /) without creating individual pages for each question.
and also how do they add the title to the end of the url without creating a new directory.
i.e. if you type in
https://stackoverflow.com/questions/7980734/
it would still take you to the question, and then will add in the title.
Thank you for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
大多数时候,使用 .htaccess 文件在请求时重写某些 URL 就足够了。例如 stackoverflow 可能会使用这样的内容:
这将使 stackoverflow.com/questsions/1234/a-title-of-a-page 和 stackoverflow.com/questions.php?q=1234 成为同一页面,因此在您的网站上您需要使用 URL 的“整洁”版本(第一个)。
可以阅读更多内容,并且您可以根据需要自定义 URL。例如,可以阅读的一些地方包括:
http://httpd。 apache.org/docs/2.0/misc/rewriteguide.html
http://www.easymodrewrite.com/< /a>
Most of the time using a .htaccess file to rewrite certain URL's when they are requested will be sufficient. For example stackoverflow might use something like this:
This would make both stackoverflow.com/questsions/1234/a-title-of-a-page and stackoverflow.com/questions.php?q=1234 the same page, so on your website you would need to use the "tidy" version of the URL (the first one)
A lot more can be read into this and you can customize you're URL's to what you require. For example, a few places to read up on it include:
http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
http://www.easymodrewrite.com/
这称为 URL 重写,本质上您有一个处理程序来解释传入的 URL。这并不意味着它是“精确”的目录结构,只是“某物”的编程路径。
服务器端,它有脚本,例如
/questions/
可能是接受 id7980734
的脚本/文件/应用程序和dynamic-url 的友好 url 链接-and-adding-a-the-title-to-the-end-of-the-page-url
足够简单,您不必使用数字 ID 来查找数据库中的记录,只需使用文字也一样,制作它URL 安全(php 函数)。
This is called URL rewriting, essentially you have a handler that interprets the URL coming in. It does not mean it is 'exact' directory structure, just programmatic path to 'something'.
Server side, it has scrips for instance
/questions/
might be the script/file/app that accepts the id7980734
and a friendly url link ofdynamic-url-and-adding-a-the-title-to-the-end-of-the-page-url
easy enough, you don't have to use numeric ID's to look up records in a db, just use text too, make it URL safe (php function for that).
我认为最简单的方法是在根文件夹中放置一个 .htaccess 文件,其内容如下:
这将在内部路由请求 http://domain.com/questions/7980734 到 http://domain.com/questions.php?id=7980734
The easiest way I think is to drop an .htaccess file in your root folder with a content something like this:
This will internally route the request http://domain.com/questions/7980734 to http://domain.com/questions.php?id=7980734
你可以用 apache mod_rewrite 来做到这一点,或者你可以看看也许MVC 框架,如 Zend,它将 URL 路由到特定的 php 控制器文件。
You can do it with apache mod_rewrite, or you can look into maybe an MVC framework like Zend which routes URLs to specific php controller files.
您必须使用enable mod_rewrite 在项目的.htaccess 中启用它。另外,您必须查看 apache 配置 httpd.conf 并检查是否添加了 mod_rewrite。因此,您可以将所有流量定向到项目中的 url 解析器。
除此之外,您的应用程序中必须有一个控制器逻辑,您可以在其中获取 url 字符串并确定 url 和参数的控制器文件。
看这里: http://httpd.apache.org/docs/current/mod/ mod_rewrite.html 了解更多信息。
you have to enable it in .htaccess of your project using enable mod_rewrite. Plus you must have a look at the apache configuration httpd.conf and check that mod_rewrite is added. So you can direct all traffic to a url parser in your project.
Other than that, you must a have a logic of controllers in your application where you get the url string and determine the controller file for the url and the parameters.
Look here : http://httpd.apache.org/docs/current/mod/mod_rewrite.html for more information.