![]() |
Mod_Rewrite How-To Guide
Apache's Mod_Rewrite How-To Guide
Helpful Resources
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 httpd.conf file manually.Create a PHP page on your web server. For demonstration purposes, call this page phpinfo.php. This file should read as follows:PHP Code:
To enable it, you need to locate Apache's configuration file. This file is called httpd.conf.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 httpd.conf open it and search for the following line. Ensure it is not commented out by removing a # symbol if it is in front of the line:Enabled: LoadModule rewrite_module modules/mod_rewrite.soDisabled: #LoadModule rewrite_module modules/mod_rewrite.soOnce 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: http://mydomain.com/index.php?page=search&type=postURL After Re-write: http://mydomain.com/search/postBenefits:
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 .htaccess. This is the file that will control the rewrite rules.For this example, this is how the final .htaccess file will look:Code:
Options +FollowSymLinkshttp://mydomain.com/news/357 and have Apache process it like this: http://mydomain.com/news.php?article_id=357.The First Part : Start the engine Code:
Options +FollowSymLinksYou should place this above your rewrite rules every time you use Mod_Rewrite. The two Options lines at the start are optional, assuming they are already set as such in your httpd.conf file. If you're not sure if your httpd.conf file is set to allow these methods, or you know they aren't included, then include them here in your .htaccess.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: Code:
RewriteRule ^{REGEX_PATTERN}$ {PAGE_ADDRESS}{PATTERN_MATCH} [FLAGS]REGEX_PATTERNFor 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. PAGE_ADDRESSYour 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 http://mysite.com/news/NEWS_ID and have my news page process it. Obviously, I don't have all these fancy directories set up to accomodate this. My news page is actually called news.php and is located in the web root (/).The PAGE_ADDRESS in this example would then be news.php?news_id=, assuming the variable I want to pass into the PHP script from the URL is news_id. It could just as easily be newsID or omgTheNews.I would then evaluate this in my news.php script as: PHP Code:
PATTERN_MATCHIn REGEX, pattern matches are reported as $n, where N is the number of the match.For example, lets say I have a Regular expression like this: Code:
^search/(.*)/(.*)/apples/(.*)/oranges/(.*)$http://mydomain.com/search/OMG/THIS/apples/IS/oranges/COOLREGEX would report the following matches: $1 = "OMG"$2 = "THIS"$3 = "IS"$4 = "COOL"In summary: (.*) - This is REGEX essentially meaning that it accepts any character into that spot within the link$1... $2... $n - This signals the REGEX expression you are relating to. So the first instance of (.*) is $1, the second instance is $2, and so on.TYING THIS TOGETHER: Let's say I wanted to use the Regular Expression I just specified ( ^search/(.*)/(.*)/apples/(.*)/oranges/(.*)$) in my website and I wanted it to redirect to a search.php. In search.php, though, I only want the fourth match set as the search ID.This would be my RewriteRule: Code:
RewriteRule ^search/(.*)/(.*)/apples/(.*)/oranges/(.*)$ search.php?search_id=$4 [L]If I were to now proceed to the web address http://mydomain.com/search/OMG/THIS/apples/IS/oranges/COOL search.php would be passed the variable search_id with a value of "COOL".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. [L] - LastIf this has been applied to a Rule then the Rewrite Engine will stop executing any remaining rules that follow. [R] - RedirectThis 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 R=301, where 301 can be any valid Redirection Status Code.[QSA] - Query String AppendOn 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. [NC] - No CaseMakes the comparison case insensitive INVOKING FLAGS 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: [L,NC]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 .htaccess file.Good Luck! |
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 http://www.this.com/that.php You could type http://www.this.com/that Code:
RewriteEngine onPassword Protect a Directory This requires two files, .htaccess and .htpasswd.htaccess Code:
AuthUserFile /home/.htpasswd If you want to give access to everyone in the .htpasswd file add this line Code:
require valid-userAuthUserFile - 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!) .htpasswd this file have lines that look like Code:
username:encryptedpasswordThe 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 Code:
steven:yH974afpUXC72 |
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) httpd -M This will output the list of loaded modules (doesn't include dynamic ones included using LoadModule directive) httpd -l |
Quote:
|
| All times are GMT +1. The time now is 05:13 PM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.