|
||||
|
|
||||
![]() Certifications ![]() Cisco ![]() IP ![]() PC ![]() Protocols ![]() RemoteAccess ![]() Security ![]() Telecommunications ![]() Tools ![]() Unix ![]() Web ![]() mindterm |
Until recently, I only had the vaguest of ideas of what mod_rewrite was, and I certainly had no clue about how to use it. So, when I started designing this site, I decided to delve into the wonders that are the mod_rewrite Apache module. So, what is mod_rewrite for?
Simply, mod_rewrite is used for rewriting a URL at the server level, giving the user
output for that final page. So, for example, a user may ask for
On workingwith.me.uk, I use mod_rewrite to redirect all pages to one central PHP page, which then loads the data that the user wanted from an external data file. Lots of people use mod_rewrite to show an “alternative” image when people are hotlinking directly to their images. What do I need to get mod_rewrite working?There’s pretty much only one thing you’ll need to get mod_rewrite working for you, and that’s to have the mod_rewrite module installed on your Apache server!
For the
purpose of this article, I’m going to assume that you don’t have access to view or edit
the Apache server httpd.conf file, so the easiest way to check whether the mod_rewrite
module is installed will be to look on your phpinfo page. If you’ve not already created
one of these for yourself, just copy and paste the following code into an new text file
using your favourite text editor, save it as <?php phpinfo(); ?> Load that page up in your web browser, and perform a search for “mod_rewrite”. All being well, you’ll find it in the “Apache loaded modules” section of the page. If it isn’t there, you’ll have to contact your hosting company and politely ask them to add it to the Apache configuration. Assuming the mod_rewrite module is loaded, then you’re good to go! A simple mod_rewrite example
So, let’s write a simple mod_rewrite example. This isn’t going to be anything fancy; we’re
just going to redirect people who ask for <html>
<head>
<title>Alice's webpage</title>
</head>
<body>
<p>
This is Alice's webpage
</p>
</body>
</html>
Upload both of these to your web server, and check that you can view both of them. Now comes the fun - we’re going to add a couple of lines to your .htaccess file. The .htaccess file is a text file which contains Apache directives. Any directives which you place in it will apply to the directory which the .htaccess file sits in, and any below it. To ours, we’re going to add the following: RewriteEngine on RewriteRule ^alice.html$ bob.html Upload this .htaccess file to the same directory as alice.html and bob.html, and reload Alice’s page. You should see Bob’s page being displayed, but Alice’s URL. If you still see Alice’s page being displayed, then check you’ve followed the instructions correctly (you may have to clear your cache). If things still aren’t working for you, then contact your technical support people and ask them to enable mod_rewrite and the FileInfo override in their httpd.conf file for you The structure of a RewriteRuleRewriteRule Pattern Substitution [OptionalFlags] The general structure of a RewriteRule is fairly simple if you already understand regular expressions. This article isn’t intended to be a tutorial about regular expressions though - there are already plenty of those available. RewriteRules are broken up as follows:
A slightly more complicated mod_rewrite exampleLet’s try a slightly more meaty example now. Suppose you have a web page which takes a parameter. This parameter tells the page how to be displayed, and what content to pull into it. Humans don’t tend to like remembering the additional syntax of query strings for URLs, and neither do search engines. Both sets of people seem to much prefer a straight URL, with no extra bits tacked onto the end.
In our example, you’ve created a main index page with takes a The following is what needs to go into your .htaccess file to accomplish that: RewriteEngine on RewriteRule ^page/([^/\.]+)/?$ index.php?page=$1 [L] Let’s walk through that RewriteRule, and work out exactly what’s going on:
Let’s write a quick page to test that this is working. The following test script will simply echo the name of the page you asked for to the screen, so that you can check that the RewriteRule is working. <html>
<head>
<title>Second mod_rewrite example</title>
</head>
<body>
<p>
The requested page was:
<?php echo $_GET['page']; ?>
</p>
</body>
</html>
Again, upload both the index.php page, and the .htaccess file to the same directory.
Then, test it! If you put the page in Conditional Statements and mod_rewriteBut what happens when you start getting people hotlinking to your images (or other files)? Hot linking is the act of including an image, media file, etc from someone else’s server in one of your own pages as if it were your own. Obviously, as a webmaster, there are plenty of times when you don’t want people doing that. You’ll almost certainly have seen examples where someone has linked to one image on a website, only for a completely different, “nasty” one to be shown instead. So, how is this done? It’s pretty simple really. All it takes are a couple of RewriteCond statements in your .htaccess file.
RewriteCond statements are as they sound - conditional statements for RewriteRules.
The basic format for a RewriteCond is Here’s how we do that: RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?somesite.com/.*$ [NC]
RewriteRule \.(gif|jpg|png)$ http://www.somesite.com/nasty.gif [R,L]
Here, the RewriteRule will only be performed if all the preceeding RewriteConds are
fulfilled. In the second RewriteCond,
The
If you simply don’t want the hot linkers to see any image at all when they hot link to your
images, then simply change the final line to
Language RedirectionProblem: how to determine which language to use when serving files, and then make it look to the user as if the files are being served out of a language-specific directory (trying to be helpful) Content negotiation wasn't the answer, as the content is dbase driven. So I wanted to append a query string to all urls, but have it be invisible to users. Solution: if ( mysite.com/ is requested ) then serve up mysite.com/index.html and determine user's preferred language from the browser. if lang. preference not set, default to english and set $lang='en' then parse all docs as such: # rewrite all requests for language-specific files RewriteEngine on Options +FollowSymlinks RewriteRule ^en/(.*)$ $1?lang=en [NC,L] RewriteRule ^de/(.*)$ $1?lang=de [NC,L] RewriteRule ^fr/(.*)$ $1?lang=fr [NC,L] so eg. all requests for german files look like this: http://mysite.com/de/valley/page.html and bookmarked links automatically come up with the correct language. Conclusionmod_rewrite is an incredibly handy tool to have in your arsenal. This article only scratched the surface of what is possible with mod_rewrite, but should have given you enough information to go out and start mod_rewriting history yourself! ReferencesApache module mod_rewrite - Apache’s big long document about the mod_rewrite module. The Definitive Guide to Apache mod_rewrite - If you’re serious about learning how to use mod_rewrite and need more detail than you got in this article, then I can sincerely recommend buying The Definitive Guide to Apache mod_rewrite. This page was created in 0.04661 seconds
Comments and Questions
Last modified: June 28 2009. |
|||