| You are here: | About>Computing & Technology>Web Design / HTML> Web Server Management> Apache> mod_rewrite> Rewrite URLs on Apache Web Servers Using mod_rewrite |
![]() | Web Design / HTML |
More About URLs and AliasesWhy Rewrite Your URLsmod_rewrite LinksHow to Set Up a DNS Alias on Apache What is mod_rewrite and How Do You Use It?Learn How to Rewrite URLs on Apache Web Servers Using mod_rewriteWhy Rewrite URLsThere are lots of reasons to rewrite URLs, but the simplest reason is to give you more control of your Web site. Using the mod_rewrite module for Apache, you can control the URLs your customers see, what pages they can access, control over who sees what pages and much more. This article won't go into the really complex aspects of mod_rewrite, but if you need to change a lot of URLs on your site or you don't want to expose what server programming you're using, mod_rewrite can be a powerful tool, and this tutorial will get you started. What is Required to Rewrite a URLThere are only a few things that are required to allow you to rewrite a URL using mod_rewrite.
Ways to Use mod_rewriteThe mod_rewrite module works by caching requests for URLs. The cached URLs are then compared to rules written in the .htaccess file (or the httpd.conf file) and for matches, applies changes to the URL. Then the request is reprocessed with the changes applied. The important thing to remember is that with mod_rewrite nothing new has been created. All mod_rewrite does is change how the server looks at whatever is being requested - such as the file name. You can use mod_rewrite to do the following:
Regular Expressions and mod_rewritemod_rewrite relies on regular expressions or regex to define the matches. While it is certainly possible to set up a mod_rewrite rule for each individual page you want selected, that would be very time consuming, and chances are you'd miss one. By using regular expressions, you can define wildcards so that more pages will be caught by one rule and sent to the correct place. For example: If you have a directory /oldpages that you have moved all the files in it to a new location /newpages you can set up a rule for every file in that directory. But if there are hundreds or thousands of pages, that will take a long time. Instead, you could write a rule that looked something like /oldpages/* and that would match every file in that directory - no matter what someone put after /oldpages/, the rule would match. There are a lot of rules in regular expressions, but here are a few basic ones:
Regular expressions are very complicated, and there are many great articles on the topic. This forum entry describes more regular expressions for mod_rewrite in detail. If you're interested in more advanced regular expressions, I highly recommend the book Mastering Regular Expressions by Jeffrey E. F. Friedl. Write Your First mod_rewrite RuleRequest for index.htm points to default.html
Point file.html to newfile.html and Note the Change
Change /page/new to /parameters.php?page=new (Hide the Parameters)
More About URLs and AliasesWhy Rewrite Your URLsmod_rewrite LinksHow to Set Up a DNS Alias on Apache |
Las Vegas on a BudgetFind a BargainHotel DealsCheap EatsFree AttractionsEntertainment for Less |
All Topics | Email Article | | | ![]() |
| Advertising Info | News & Events | Work at About | SiteMap | Reprints | Help | Our Story | Be a Guide |
| User Agreement | Ethics Policy | Patent Info. | Privacy Policy | ©2008 About, Inc., A part of The New York Times Company. All rights reserved. |


