Contact Our Development Team
Free Code Tutorials & Open Source Code
Apache Mod Rewrite - A Short Tutorial
Tips & Tricks > Apache Mod Rewrite
Apache Mod Rewrite
These are just a few useful tricks with mod_rewrite module for Apache...
# 1. The Basics
#
RewriteRule ^folder/path.html$ path.php [L]
# This rule above will take:
# http://www.yoursite.com/folder/path.html
# and covert it to
# path.php
##############################################
#
# In general this is the pattern
# RewriteCond {PATTERN_TO_MATCH} {SUBJECT_REGEX_FORM}
# RewriteRule {REGEX} {ACTUAL_SERVER_PATH} [{COMMAR,SEPERATED,CONDITIONS}]
#
##############################################
# RewriteCond - These are optional, and refer to the next RewriteRule ONLY
# This information can be added to .htaccess file or appended to httpd.conf or include file.
# In .htaccess, it should look like the following...
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^folder/path.html$ path.php [L]
#
#
# Lastly, the commar seperated conditions tell apache what to imply in terms of behaviour.
# [L] - On matching this rule, finish and dont try to match anything further
# [NC] - Case Insensitive (No Case) with regards to the {REGEX}
# [R=301] - Redirect to this page. The 301 is the status code sent to the browser.
# Other Mod_Rewrite_Flags
# [C] - Chain
# [E] - Environmental Variable
# [F] - Forbidden
# [G] - 410 Gone
# [N] - Next Round
# [NE] - No Escape
# [NS] - No SubRequest
# [P] - Proxy
# [PT] - Pass Through
# [QSA] - Query String Append
# [S] - Skip
# [T] - Type
# These can be combined....
# [R=301,NC,L]
#
# For more information, please visit the apache website! The documentation is pretty good!
# 2. Forward Subdomain To Page
#
# Please note for this to work, you should have *.yoursite.com, yoursite.com forwarded...
# If your using Virtual Hosts, it might look a bit like this in httpd.conf
<VirtualHost *:80>
        ServerName      www.yoursite.com
        ServerAlias     *.yoursite.com yoursite.com
        ErrorLog        logs/com.yoursite.www.err.log
        CustomLog       logs/com.yoursite.www.acc.log common
        DocumentRoot    /home/yoursite/www
        DirectoryIndex  index.php
</VirtualHost>
#
RewriteCond %{HTTP_HOST} ^(?:www\.)?([a-zA-Z0-9]+)\.yoursite\.com
RewriteRule (.*) http://www.yoursite.com/%1 [R=301,L]
#
# Notice the %1, this is a wildcard variable taken from the RewriteCond above!
# %1 refers to the first return in the statement = ([a-zA-Z0-9]+)
# The first part (?:www\.)?, the ?: simply means no return. While the ? = none or one.
#
# http://www.something.yoursite.com will forward to http://www.yoursite.com/something
# http://something.yoursite.com will also forward to http://www.yoursite.com/something
# 3. TLD to www.tld - Useful for good SEO
#
RewriteCond %{HTTP_HOST} ^yoursite\.com
RewriteRule (.*) http://www.yoursite.com/$1 [R=301,L]
#
# The $1 refers to the information (.*)
# This means...
# http://yoursite.com/folder/page.html will automatically forward to:
# http://www.yoursite.com/folder/page.html
# 4. Page Path Rewriting...
#
RewriteRule ^(.*?)/(.*?)/(.*?)[/]?$ http://www.yoursite.com/page.php?c=$1&i=$2&t=$3 [L]
#
# So.. with this rule:
# http://www.yoursite.com/tv/cables/hdmi
# Would overlay:
# http://www.yoursite.com/page.php?c=tv&i=cables&t=hdmi
# 5. How to combine everything... Easy but probably worth mentioning!
#
# Your .htaccess file should look like:
#
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^folder/path.html$ path.php [L]
RewriteCond %{HTTP_HOST} ^(?:www\.)?([a-zA-Z0-9]+)\.yoursite\.com
RewriteRule (.*) http://www.yoursite.com/%1 [R=301,L]
RewriteCond %{HTTP_HOST} ^yoursite\.com
RewriteRule (.*) http://www.yoursite.com/$1 [R=301,L]
RewriteRule ^(.*?)/(.*?)/(.*?)[/]?$ http://www.yoursite.com/page.php?c=$1&i=$2&t=$3 [L]
#
# Enjoy!
There is a huge amount that can be done with mod_rewrite. Its is a very useful tool for easily readable paths and great for SEO! Please feel to add your own rule examples below in the comments section...
Page Responses
Posted: 24th September at 2:50pm
Regexs:
() = Return
[] = Character
{} = Length
? = {0,1}
+ = {1,}
* = {0,}
^ = Start of string
$ = End of string
([^a]+) on bbbbbcccddddfffaaaaa would return bbbbbcccddddfff because ([^a]+) means return anything until 'a' is found.
If you have anything to contribute to this tutorial, found a bug, or know a better way of achieving the same goal, please leave your response below.
     
Copyright ©2009, Wired IDS Ltd. | Licensed under Creative Commons Attribution Share-Alike | Load time: 0.3049 seconds