Mod_Rewrite How-To Guide
Apache's Mod_Rewrite How-To Guide
Editorial Note: If you find anything in this guide that is incorrect, or would like content added, please Send a PM to ShaneC. Mod_Rewrite questions should be posted in the Apache Configuration forum.
Note: Guide Assumptions
This guide assumes that you're using the following:
Important: Make sure Mod_Rewrite is Installed and Enabled
On some web hosts Mod_Rewrite is not enabled by default.
To check if mod_rewrite is installed:
Note: In most PHP configurations you can detect if Mod_Rewrite is installed with the method below. If, however, you're using CGI / SuPHP you will have to check the Apache
Create a PHP page on your web server. For demonstration purposes, call this page
To enable it, you need to locate Apache's configuration file. This file is called
Note: If you're on a shared host and don't have access to the httpd.conf file for the Apache Server you will need to contact your web host and request they enable the mod_rewrite module.
Once you've located
Once you've enabled the line you will need to restart your Apache server in order for it to take effect.
Understanding what Mod Rewrite does
Mod_Rewrite is the process of converting from URLs filled with variables to clean URLs:
URL Before Re-write:
URL After Re-write:
The Server (aka Apache) doesn't understand http://mydomain.com/page/about-us/27. To that it will understand it as folders and sub-directories.
Essentially what you are doing is making it so all the links the user sees are the nice, clean ones. The only time the long index.php?p=about...etc. links appear are internally to apache - never seen by the user.
Setting up the Rewrite
Create a file, if there isn't already, in the root of your web directory (/) that is called
For this example, this is how the final
The First Part : Start the engine
You should place this above your rewrite rules every time you use Mod_Rewrite.
The Second Part : Rules to live by
Rewrite Rules are created using Regular Expressions. They tell Apache under what patterns certain actions should be performed.
We will go in depth on them in the next section.
Rewrite Rules: Tell Apache what to do
Rewrite rules take the following format:
For simplicity sake, this guide assumes you already have a good working knowledge of REGEX. If you don't, I highly recommend you read about Regular Expressions (REGEX) and how to create them. You can also download The Regex Coach for testing your REGEX and seeing if it will match the patterns you'd like it to.
The REGEX Pattern is the Regular Expression string that apache will try to match every time a user types in a URL on your domain. If it matches this expression, then the RewriteRule is followed.
Your page address is exactly that, the actual literal path of the target page on your server.
For example, let's take our earlier instance. I want to be able to type in
The PAGE_ADDRESS in this example would then be
I would then evaluate this in my news.php script as:
In REGEX, pattern matches are reported as
For example, lets say I have a Regular expression like this:
REGEX would report the following matches:
TYING THIS TOGETHER:
Let's say I wanted to use the Regular Expression I just specified (
This would be my RewriteRule:
If I were to now proceed to the web address
Rewrite Rules: Flag them down
Flags are your way of further customizing what your RewriteRules do. There are many different flags available, see the Mod Rewrite Cheat Sheet for a detailed listing, but we'll only cover the common ones here.
If this has been applied to a Rule then the Rewrite Engine will stop executing any remaining rules that follow.
This will execute a redirect and send a status code as well as the new location to the user's browser. This will then cause the client browser to update the address bar with the new destination. This flag can, optionally, take in a status code by invoking it as such
On dynamic pages the query string contains GET data (ie. everything after the ?). If you do not include a query string in the destination then the original query is automatically transferred. If you do specify a query string destination then this overwrites the original. If you wish to add to the query string and keep original data intact, you must use the QSA flag.
Makes the comparison case insensitive
Invoking flags is done easily. Simply encase them in square brackets ("[" and "]") and separate them by a comma.
For example, if I wanted to apply a Last and No Case flag to a rule:
Extra Learning and Reference
For a more detailed tutorial, complete with more examples, see the Easy Mod Rewrite Guide.
Moreover, if you have any questions then you're at the right place! Simply start a topic stating your problem/question and we'll do our best to help. Please try to be as clear as possible, and post all relevant lines of your code as well as your
Often times I come across the same questions asked in this part of the forum. I thought it might be a good reference to have these answers in one location.
Hide file Extension
This will hide specified file extensions from files. The below code will hide .php extensions.
So in order to go to
You could type
Password Protect a Directory
This requires two files,
If you want to give access to everyone in the .htpasswd file add this line
AuthUserFile - path to .htpasswd (this file contains usernames and paswords)
AuthName - is the name displayed when logging into the directory
AuthType - basic authentication allows the use restrict access by looking up users in plain text password files (plain text pass files are very dangerous!)
this file have lines that look like
The password is displayed in an encrypted format. The password encryption is the same as PHP's crypt() function. It is not reversible.
An example would be
Good tutorials for mod rewrite, could you suggest me guideline for window IIS server?. I found very hard in case of windows IIS server. Thanks
Nice howto ShaneC.
When running your own server (vps/dedicated) you can use the following commands to check whether mod rewrite is on or off.
This will output the list of loaded modules (static & dynamic)
This will output the list of loaded modules (doesn't include dynamic ones included using LoadModule directive)
Good tutorials for mod rewrite
Good tutorials for mod rewrite
|All times are GMT +1. The time now is 07:52 PM.|
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.