引用mod_rewrite的URL重写和pretty的链接和说明 [英] Reference: mod_rewrite, URL rewriting and "pretty links" explained

本文介绍了引用mod_rewrite的URL重写和pretty的链接和说明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

pretty的链接通常被请求的话题,但很少充分说明。 mod_rewrite的是一种方法,使pretty的联系,但它的复杂,它的语法非常简洁,很难神交和文档假定在HTTP一定熟练程度的。可以用简单的术语有人解释pretty的链接是如何工作的,以及如何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?

其他常见的名称,别名,条款对清洁网址:REST风格的URL,用户友好的URL,搜索引擎友好的URL,段塞,MVC的URL(可能是用词不当)

Other common names, aliases, terms for clean urls: RESTful URLs, User-friendly URLs, SEO-friendly URLs, Slugging, 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服务器的请求的网址 /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/foo/bar.html 存在,如果是为它服务。如果文件中的.PHP结尾它将调用PHP跨preTER和然后的返回结果。所有这些关联是完全可配置的;文件没有结束在.PHPWeb服务器通过PHP跨preTER运行它,而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服务器接收到的网址 /富/酒吧的要求,你可以的 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

这条规则说的只要有请求匹配/富/酒吧,改写为/富/巴兹。的请求将被处理,就好像 /富/巴兹已被要求代替。这可用于各种效果,例如:

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。换句话说,如果 /富/酒吧被请求的URL,它会被处理,就好像 /foo/bar.html 已要求。请参见 HTTP://regular-ex$p$pssions.info 有关正EX pression匹配的详细信息,捕获和更换。

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。也就是说,任何和所有请求进来,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.

mod_rewrite的不会奇迹般地使所有的网址pretty的。这是一个普遍的误解。如果您在您的网站此链接:

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可以做,使该pretty的。为了使这个pretty的链接,你必须:

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

  1. 更​​改链接到pretty的链接:

  1. Change the link to a pretty link:

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

  • 使用mod_rewrite的服务器处理请求的URL /我/ pretty的/链接使用上述任何一种方法。

  • 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资源usally更多的努力。

    One could utilize mod_substitute in conjunction to transform outgoing HTML pages and their contained links. Though this is usally more effort than just updating your HTML resources.

    有很多的mod_rewrite可以做的,非常复杂的匹配规则,你可以创建,其中包括链接数改写,代理请求到一个完全不同的服务或设备,返回特定的HTTP状态codeS作为响应,重定向请求等,这是非常功能强大,可以用来伟大的好,如果你理解了基本的HTTP请求 - 响应机制。它的没有的自动让你的链接pretty的。

    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重写和pretty的链接和说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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