参考:mod_rewrite、URL 重写和“漂亮链接";解释 [英] Reference: mod_rewrite, URL rewriting and "pretty links" explained

查看:44
本文介绍了参考:mod_rewrite、URL 重写和“漂亮链接";解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

漂亮的链接"是一个经常被要求的话题,但很少被完全解释.mod_rewrite 是制作漂亮链接"的一种方式,但它很复杂,而且语法非常复杂简洁,难以理解,并且文档假定您对 HTTP 具有一定的熟练程度.有人可以简单解释一下漂亮链接"是如何工作的,以及如何使用 mod_rewrite 来创建它们?

"Pretty links" is an often requested topic, but it is rarely fully explained. mod_rewrite is one way to make "pretty links", but it's complex and its syntax is very terse, hard to grok, and the documentation assumes a certain level of proficiency in HTTP. Can someone explain in simple terms how "pretty links" work and how mod_rewrite can be used to create them?

其他常用名称、别名、干净 URL 的术语:RESTful URL、用户-友好的 URL,SEO 友好的 URL,slugging 和 MVC URL(可能用词不当)

Other common names, aliases, terms for clean URLs: RESTful URLs, user-friendly URLs, SEO-friendly URLs, slugging, and MVC URLs (probably a misnomer)

推荐答案

要了解 mod_rewrite 的作用,您首先需要了解 Web 服务器的工作原理.Web 服务器响应 HTTP 请求.最基本级别的 HTTP 请求如下所示:

To understand what mod_rewrite does you first need to understand how a web server works. A web server responds to HTTP requests. An HTTP request at its most basic level looks like this:

GET /foo/bar.html HTTP/1.1

这是浏览器向 Web 服务器发出的简单请求,从它请求 URL /foo/bar.html.需要强调的是,它不请求文件,它只请求一些任意的 URL.该请求也可能如下所示:

This is the simple request of a browser to a web server requesting the URL /foo/bar.html from it. It is important to stress that it does not request a file, it requests just some arbitrary URL. The request may also look like this:

GET /foo/bar?baz=42 HTTP/1.1

这与对 URL 的请求一样有效,更明显的是它与文件无关.

This is just as valid a request for a URL, and it has more obviously nothing to do with files.

Web 服务器是一个侦听端口的应用程序,接受来自该端口的 HTTP 请求并返回响应.Web 服务器可以完全自由地以它认为合适的任何方式/以您配置它响应的任何方式响应任何请求.此响应不是文件,而是 HTTP 响应,它可能与任何磁盘上的物理文件有关,也可能无关.Web 服务器不一定是 Apache,还有许多其他 Web 服务器都只是持久运行的程序,并连接到响应 HTTP 请求的端口.你可以自己写一个.本段旨在让您摆脱 URL 直接等同于文件的任何概念,这对于理解非常重要.:)

The web server is an application listening on a port, accepting HTTP requests coming in on that port and returning a response. A web server is entirely free to respond to any request in any way it sees fit/in any way you have configured it to respond. This response is not a file, it's an HTTP response which may or may not have anything to do with physical files on any disk. A web server doesn't have to be Apache, there are many other web servers which are all just programs which run persistently and are attached to a port which respond to HTTP requests. You can write one yourself. This paragraph was intended to divorce you from any notion that URLs directly equal files, which is really important to understand. :)

大多数 Web 服务器的默认配置是在硬盘上查找与 URL 匹配的文件.如果服务器的文档根被设置为,比如,/var/www,它可能会查看文件/var/www/foo/bar.html 存在并在存在时提供它.如果文件以.php"结尾,它将调用 PHP 解释器并然后返回结果.所有这些关联都是完全可配置的;文件不必以.php"结尾,Web 服务器就可以通过 PHP 解释器运行它,并且 URL 不必与磁盘上的任何特定文件匹配才能发生.

The default configuration of most web servers is to look for a file that matches the URL on the hard disk. If the document root of the server is set to, say, /var/www, it may look whether the file /var/www/foo/bar.html exists and serve it if so. If the file ends in ".php" it will invoke the PHP interpreter and then return the result. All this association is completely configurable; a file doesn't have to end in ".php" for the web server to run it through the PHP interpreter, and the URL doesn't have to match any particular file on disk for something to happen.

mod_rewrite 是一种重写内部请求处理的方法.当 Web 服务器收到对 URL /foo/bar 的请求时,您可以重写该 URL 为其他内容,然后 Web 服务器将在磁盘上查找文件以进行匹配它.简单例子:

mod_rewrite is a way to rewrite the internal request handling. When the web server receives a request for the URL /foo/bar, you can rewrite that URL into something else before the web server will look for a file on disk to match it. Simple example:

RewriteEngine On
RewriteRule   /foo/bar /foo/baz

这条规则说每当请求匹配/foo/bar"时,将其重写为/foo/baz".然后请求将被处理为/foo/baz 已被请求.这可以用于各种效果,例如:

This rule says whenever a request matches "/foo/bar", rewrite it to "/foo/baz". The request will then be handled as if /foo/baz had been requested instead. This can be used for various effects, for example:

RewriteRule (.*) $1.html

此规则匹配任何内容 (.*) 并捕获它 ((..)),然后将其重写为附加.html"".换句话说,如果 /foo/bar 是请求的 URL,它将被处理为好像 /foo/bar.html 已被请求.有关正则表达式匹配、捕获和替换的更多信息,请参阅 http://regular-expressions.info.

This rule matches anything (.*) and captures it ((..)), then rewrites it to append ".html". In other words, if /foo/bar was the requested URL, it will be handled as if /foo/bar.html had been requested. See http://regular-expressions.info for more information about regular expression matching, capturing and replacements.

另一个经常遇到的规则是:

Another often encountered rule is this:

RewriteRule (.*) index.php?url=$1

这再次匹配任何内容并将其重写到文件 index.php 并在 url 查询参数中附加最初请求的 URL.即,对于任何和所有传入的请求,文件 index.php 将被执行,该文件将可以访问 $_GET['url'] 中的原始请求,因此它可以做任何它想做的事情与它.

This, again, matches anything and rewrites it to the file index.php with the originally requested URL appended in the url query parameter. I.e., for any and all requests coming in, the file index.php is executed and this file will have access to the original request in $_GET['url'], so it can do anything it wants with it.

首先,您将这些重写规则放入网络服务器配置文件.Apache 还允许*您将它们放入文档根目录中名为 .htaccess 的文件中(即在 .php 文件旁边).

Primarily you put these rewrite rules into your web server configuration file. Apache also allows* you to put them into a file called .htaccess within your document root (i.e. next to your .php files).

* If 主要 Apache 配置文件允许;它是可选的,但通常启用.

* If allowed by the primary Apache configuration file; it's optional, but often enabled.

mod_rewrite 不会神奇地使您的所有 URL 变得漂亮".这是一个普遍的误解.如果您的网站中有此链接:

mod_rewrite does not magically make all your URLs "pretty". This is a common misunderstanding. If you have this link in your web site:

<a href="/my/ugly/link.php?is=not&amp;very=pretty">

mod_rewrite 无法让它变得漂亮.为了使它成为一个漂亮的链接,您必须:

there's nothing mod_rewrite can do to make that pretty. In order to make this a pretty link, you have to:

  1. 把链接改成漂亮的链接:

  1. Change the link to a pretty link:

<a href="/my/pretty/link">

  • 在服务器上使用 mod_rewrite 处理对 URL /my/pretty/link 的请求,使用上述任何一种方法.

  • Use mod_rewrite on the server to handle the request to the URL /my/pretty/link using any one of the methods described above.

    (可以使用 mod_substitute 结合转换传出的 HTML 页面及其包含的链接.虽然这通常比更新 HTML 资源更费力.)

    mod_rewrite 可以做很多事情,你可以创建非常复杂的匹配规则,包括链接多次重写、将请求代理到完全不同的服务或机器、返回特定的 HTTP 状态代码作为响应、重定向请求等.它非常强大并且可以如果您了解基本的 HTTP 请求 - 响应机制,就会很好地习惯.它不会自动使您的链接变得漂亮.

    There's a lot mod_rewrite can do and very complex matching rules you can create, including chaining several rewrites, proxying requests to a completely different service or machine, returning specific HTTP status codes as responses, redirecting requests etc. It's very powerful and can be used to great good if you understand the fundamental HTTP request-response mechanism. It does not automatically make your links pretty.

    有关所有可能的标志和选项,请参阅官方文档.

    See the official documentation for all the possible flags and options.

    这篇关于参考:mod_rewrite、URL 重写和“漂亮链接";解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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