Mod rewrite Tutorials
overview - syntax - basic - medium - advanced
Mod Rewrite Basic tutorials
The majority of you came to this site to find out how to do one of these
basic tutorials. So lets go ahead and line up what we'll be learning.
Tutorial 1
Simple Static to Dynamic Rewrites
It's easier than you think. Out of all the rewrites I've done I'd
say I still do this type of rewrite the most. If you are working on
a non commercial web applications then you should
be able to implement this type of mod rewrite in just a few minutes.
Scenario: We have a dynamic site that out puts query
strings in the urls. We want to change these dynamic urls to static search
engine friendly urls. All the current web pages use a .php extension. We
want the urls to look like a plain html page with a .html ext.
Current urls:
- section.php?sec_id=123
- category.php?cat_id=123&num_limit=10
- product.php?prod_id=123&prod_prod_sku=MET345B
&
prod_name=metal
Desired urls:
- section-123.html
- category/123-10.html
- product/123-MET345B-metal.html
Step 1
Change the code in your .php files to stop outputting dynamic urls. If
you made the file then this should be too hard. If your development team
or freelance programmer made these urls then you'll have to hack it out
or get them to do it.
Here is some sample php code that might help you in your hunt for changing
the link out puts.
section.php?sec_id=123 <a href="section.php?sec_id=<?php
echo $sec_id ?>"><?php echo $sec_name ?></a>
change to
section-123.html <a href="section-<?php
echo $sec_id ?>.html"><?php echo $sec_name ?></a>
category.php?cat_id=123&num_limit=10 <a
href="category.php?cat_id=<?php echo $cat_id ?>&num_limit=<?php
$num_limit ?>"><?php echo $cat_name ?></a>
change to
category/123-10.html <a href="category/<?php
echo $cat_id ?>-<?php echo $num_limit ?>.html"><?php
echo $cat_name ?></a>
product.php?prod_id=123&prod_sku=MET345B&prod_name=metal <a
href="<?php echo $prod_id ?>&prod_sku=<? echo $prod_sku
?>&prod_name=<?php echo $prod_name ?>"><?php
echo $prod_name ?></a>
change to
product/123-MET345B-metal.html <a href="product/<php
echo $prod_id ?>-<?php echo $prod_sku ?>-<?php echo $prod_name
?>.html"><?php echo $prod_name ?></a>
Note: When you change a url to be called from a sub folder
you're going to have to change your image, css, external javascript files
and any other media or include that will be in the page. The easiest way
to do this is to use absolute urls http://www.yoursite.com/images/ or http://www.yoursite.com/css/file.css
to call these in. This part usually drive people nuts!
Step 2
Once you've changed the code to out put the desired urls we can start
writing the code for the .htaccess file
First we'll start by turning the rewrite engine on
.htaccess code RewriteEngine On
Next we'll set the RewriteBase to our root folder for our mod rewrite
block.
.htaccess code RewriteEngine On
RewriteBase /
Alright now that we've turned on the rewrite engine and set our base directory
lets go ahead and make our first rule.
First we look for the pattern we need to match. Our first url is section-123.html
since only section is on the root of the site and it will always begin
with the word section- we'll start our pattern matching looking for exactly
that RewiteRule ^section- .
Now we have to grab the 123 and reference that to be used in the rewrite
block. We'll just use a character class to search for any number
that repeats 1 or more times ([0-9]+).
Remember the brackets! That lets the rewrite block call
in those numbers when the time comes.
Last but not least we'll go ahead and finish the matching by tacking on
the \.html$ so the rewrite engine knows to stop matching at this point.
Our rule should look like this now RewriteRule ^section-([0-9]+)\.html$
Ok to finalize this rule we need to tell the rewrite block to rewrite
as an alias back to section.php then we need to call in all the references
() from the pattern block that we collected. In this rule
there is only one reference so we call it in as a variable $1.
Your rewriterule should look like this now
RewriteRule ^section-([0-9]+)\.html$ section.php?sec_id=$1
Now we just add the flag [L] to tell the mod rewrite that this is the
last rule it needs to parse and we're done.
Your code should look like this at this point
.htaccess code RewriteEngine On
RewriteBase /
RewriteRule ^section-([0-9]+)\.html$ section.php?sec_id=$1 [L]
We should be able to move fairly quickly now through our next rules since
we'll follow the same pattern above
Since our next url category/123-10.html will always start with category
we'll use ^category/ to start our rewrite rule.
Next We'll need to find any number 1 or more times between the / and -
and reference it. We do that with the familiar ([0-9]+). And then we'll
need to match the next set of numbers between the - and the .html. We just
use the same class as before ([0-9]+).
We finish off our pattern block with a \.html$ and our code should now
look like this RewriteRule ^category/([0-9]+)-([0-9]+)\.html$
We've made 2 references this time these are easily passed to the rewrite
block as $1 and $2 to complete the rewrite. As before we tack
on the [L] flag and we're done. Our htacces file should now look like
this.
.htaccess code RewriteEngine On
RewriteBase /
RewriteRule ^section-([0-9]+)\.html$ section.php?sec_id=$1 [L}
RewriteRule ^category/([0-9]+)-([0-9]+)\.html$ category.php?cat_id=$1&num_limit=$2
[L]
Alright we're on to our final rewrite rule. This should take us long.
By now were pretty much a mod rewrite guru. So we make it quick
product/123-MET345B-metal.html is the url
^product/
This will start us off on a good pattern match
([0-9]+)-
This will match and reference the first numbers
([^-]+)-
Since the sku is in between dashes we just need everything that's not a dash.
Tricky tricky. negate the class and look for anything but a dash. ha!
(.+)\.html
Since we don't know if the name will have _'s or -'s in them we'll just do
a wildcard search and grab everything between the - and .html.
Now that our pattern block is squeaky clean lets send
our 3 references over the rewrite block so we can finish
this up. product.php?prod_id=$1&prod_sku=$2&prod_name=$3
Note: You do not have to call your references in order in
the rewrite block. You can call them $2 $3 $1 if you want just remember
$1 = first () in the pattern block etc.
Our htaccess file should now look like this
.htaccess code RewriteEngine On
RewriteBase /
RewriteRule ^section-([0-9]+)\.html$ section.php?sec_id=$1 [L}
RewriteRule ^category/([0-9]+)-([0-9]+)\.html$ category.php?cat_id=$1&num_limit=$2
[L]
RewriteRule ^product/([0-9]+)-([^-]+)-(.+)\.html$ product.php?prod_id=$1&prod_sku=$2&prod_name=$3
[L]
And we're done. You should be able to upload the .htaccess file to the
server first and test the urls out before you upload your .php files. That
way you don't bring down a live site if you've made a typo <-- which
does happen!
<< mod rewrite syntax :: basic
mod rewrites :: medium mod rewrites >> |