apache - Reference: mod_rewrite, URL rewriting and "pretty links" explained -
"pretty links" requested topic, explained. mod_rewrite 1 way make "pretty links", it's complex , syntax terse, hard grok , documentation assumes level of proficiency in http. can explain in simple terms how "pretty links" work , how mod_rewrite can used create them?
other common names, aliases, terms clean urls: restful urls, user-friendly urls, seo-friendly urls, slugging, mvc urls (probably misnomer)
to understand mod_rewrite first need understand how web server works. web server responds http requests. http request @ basic level looks this:
get /foo/bar.html http/1.1
this simple request of browser web server requesting url /foo/bar.html
it. important stress not request file, requests arbitrary url. request may this:
get /foo/bar?baz=42 http/1.1
this valid request url, , has more nothing files.
the web server application listening on port, accepting http requests coming in on port , returning response. web server entirely free respond request in way sees fit/in way have configured respond. response not file, it's http response may or may not have physical files on disk. web server doesn't have apache, there many other web servers programs run persistently , attached port respond http requests. can write 1 yourself. paragraph intended divorce notion urls directly equal files, important understand. :)
the default configuration of web servers file matches url on hard disk. if document root of server set to, say, /var/www
, may whether file /var/www/foo/bar.html
exists , serve if so. if file ends in ".php" invoke php interpreter , then return result. association configurable; file doesn't have end in ".php" web server run through php interpreter, , url doesn't have match particular file on disk happen.
mod_rewrite way rewrite internal request handling. when web server receives request url /foo/bar
, can rewrite url else before web server file on disk match it. simple example:
rewriteengine on rewriterule /foo/bar /foo/baz
this rule says whenever request matches "/foo/bar", rewrite "/foo/baz". request handled if /foo/baz
had been requested instead. can used various effects, example:
rewriterule (.*) $1.html
this rule matches (.*
) , captures ((..)
), rewrites append ".html". in other words, if /foo/bar
requested url, handled if /foo/bar.html
had been requested. see http://regular-expressions.info more information regular expression matching, capturing , replacements.
another encountered rule this:
rewriterule (.*) index.php?url=$1
this, again, matches , rewrites file index.php requested url appended in url
query parameter. i.e., , requests coming in, file index.php executed , file have access original request in $_get['url']
, can wants it.
what mod_rewrite not do
mod_rewrite not magically make urls "pretty". common misunderstanding. if have link in web site:
<a href="/my/ugly/link.php?is=not&very=pretty">
there's nothing mod_rewrite can make pretty. in order make pretty link, have to:
change link pretty link:
<a href="/my/pretty/link">
use mod_rewrite on server handle request url
/my/pretty/link
using 1 of methods described above.
(one use mod_substitute
in conjunction transform outgoing html pages , contained links. though usally more effort updating html resources.)
there's lot mod_rewrite can , complex matching rules can create, including chaining several rewrites, proxying requests different service or machine, returning specific http status codes responses, redirecting requests etc. it's powerful , can used great if understand fundamental http request-response mechanism. not automatically make links pretty.
see official documentation possible flags , options.
Comments
Post a Comment