I recently added a function to my commercial PHP framework which allows unlimited parameters in a mod_rewrite rewritten URL, without having to add more lines to do so. In this tutorial I will explain to you how to create this function and use it.
Table of Content:
- mod_rewrite: what is it and how to use it
- Unlimited parameters
1. mod_rewrite: What is it & how to use it
mod_rewrite is an apache module that allows you to rewrite URLs on the fly. You can create redirects with it or make dynamic pages appear as static files. Say you have the following URL:
http://example.com/index.php?act=news&code=view&id=3
You can make it look much more friendly, without the query string and characters such as ? and &. mod_rewrite can make it look like this:
http://example.com/news/view/1/
You can even make it appear as a .html file:
http://example.com/news/view/1.html
How do you do it? Look at the example below:
|
RewriteEngine On
RewriteRule ^news/view/(.*).html$ index.php?act=news&code=view&id=$1 |
The code above is put in a .htaccess file.
What it does: we turn on the RewriteEngine, then tell it to rewrite an url to another.
mod_rewrite uses regex for its RewriteRule statements.
^ means start of the search and $ means end.
(.*) means it looks for 0 or more characters, can be any characters.
For every parameter you add you can use a $ variable.
Since we have only one, our (.*) will be $1.
We have to escape the .html with a because a dot is a regex keyword. The rewrite rule above allows any characters, not just numeric or strings. What we would rather do in this case, is make it numeric:
RewriteRule ^news/view/([0-9]+).html$ index.php?act=news&code=view&id=$1
This rule is applied when the first parameter is numeric and 1 character or more. If you input a non-numeric character it will give you a 404.
http://example.com/news/view/fdjghdfjkg.html
This would give you a 404.
In order to create friendly URLs for all of your website's system you'd have to keep adding news lines. Sometimes this can be a real pain. I've come up with a solution: A PHP system that allows you to set unlimited parameters in your urls, without having to add more mod_rewrite lines.
Read on to learn about it!
2. Unlimited Parameters
This chapter of the tutorial is about the function I've made.
I've constructed a function setInput() , which puts all of the data from the mod_rewritten URL in $_GET.
Add the following lines to your .htaccess file:
|
RewriteEngine On
RewriteRule ^(.*).html$ index.php?string=$1 |
What this does:
http://example.com/news/view/my-title.html
Is actually sent to:
http://example.com/index.php?string=news/view/my-title
We have to:
- explode $_GET['string'] at character / to get an array of data
- make a key/value kind of system to assign data to a key
- assign the first parameter as our action (ie: news or tutorials)
- empty $_GET and put the whole array in $_GET
We're calling our function setInput() .
Let's see what we have so far:
|
function setInput()
{ } |
Let's continue! First up we have to explode() our $_GET string.
Look at the code below:
|
$input = explode( '/', $_GET['string'] );
We explode it with the directory seperator character. Next up we have to assign a value to a key. We'll use the for() loop to loop through our array. for( $i = 0; $i < count( $input ); $i++ ) { if( $i != 0 ) { $input_array[ $input[ $i ] ] = $input[ $i + 1 ]; $i++; } } |
Essentially, what we do here is loop through $input, check if this isn't the first parameter (we're skipping that for other purposes, I will explain it later).
We put it in another array, $input_array. The key is the parameter of the current $i value, and the value is the parameter of the next $i value. We skip the next parameter by doing $i++, so we don't a wrong input array.
| $input_array['act'] = $input[0]; |
What we do here, is set the first parameter's key to 'act'.
This way, http://example.com/news.html, the 'act' key would have the value 'news'.
|
foreach( $input_array as $k => $v )
{ if( $v != '' ) { $newinput[ $k ] = $v; } } $input_array = $newinput; |
This is something I myself like to do: strip empty parameters.
You can leave this out of you like, but personally I prefer having this.
|
unset( $_GET );
$_GET = $input_array; |
What we do here is empty the whole array of $_GET and put our new input array here.
You can now use $_GET to get the data from the URL parameters!
That's basically all of it! Below you will find the full function code:
|
function setInput( $var = '' )
{ $input = explode( '/', $_GET['string'] ); for( $i = 0; $i < count( $input ); $i++ ) { if( $i != 0 ) { $input_array[ $input[ $i ] ] = $input[ $i + 1 ]; $i++; } } $input_array['act'] = $input[0]; unset( $_GET ); $_GET = $input_array; } ?> |
I would really appreciate it if you actually read the whole tutorial and learnt something, instead of just scrolled down and copied the code!
Thank you for reading and see you soon with the next tutorial!






