我如何在PHP中进行URL重写? [英] How do I do URL rewriting in php?

查看:145
本文介绍了我如何在PHP中进行URL重写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个双重问题:正确的答案将转到如何在PHP中执行,解释是否有任何优势,如果可能的话也会计数。

This is a double question: The correct answer will go to "How you do it in PHP" explaining if there is any advantage goes also counts if possible.

我只是好奇,因为我真的不知道,我在网页上看到很多。

I'm just curious because I really don't know and I see it a lot in webpages.

编辑:我不知道技术名称,但例如这里的Stackoverflow:
http://stackoverflow.com/posts/edit/522452 就是我的意思是文件夹(这个术语以前在问题的标题中使用过)。

I don't know the technical name, but for example here on Stackoverflow: "http://stackoverflow.com/posts/edit/522452" is what I mean as "folders" (a term previously used in the title of the question).

推荐答案

如果您引用 / posts / edit / 522452 风格的网址,而不是 /posts.asp?action=edit&postid=522452 风格的网址(或不管它转换为后端),通常通过URL重写器来完成,比如 mod_rewrite的 。该URL的规则可能如下所示:

If you're referring to /posts/edit/522452-style URLs as opposed to /posts.asp?action=edit&postid=522452-style URLs (or whatever it translates to on the back end), this is typically done through a URL rewriter, such as mod_rewrite. A rule for that URL might look like this:

RewriteRule ^/posts/(\w+)/(\d+) /posts.asp?action=\1&postid=\2

两个主要优点这种类型的网址是:

The two primary advantages to this kind of URL are that:


  1. 文件夹类型的网址对于人们来说更容易输入和记忆。 b $ b
  2. 页面看起来像是HTTP代理的页面。传统上,代理不会使用参数缓存页面,因为它们不代表单独的内容并且频繁更改(认为搜索结果)。使用文件夹式URL可以正常缓存它们。

在PHP中,您可以通过 $ _ GET ['action'] $ _ GET ['postid'] ,就好像浏览器要求重写的表单一样。

In PHP, you can then access these options via $_GET['action'] and $_GET['postid'], exactly as if the browser had asked for the rewritten form.

这篇关于我如何在PHP中进行URL重写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆