当前位置: 代码迷 >> 综合 >> Create Dynamic URLs With Mod_Rewrite and PHP Functions
  详细解决方案

Create Dynamic URLs With Mod_Rewrite and PHP Functions

热度:94   发布时间:2023-12-08 00:59:42.0

A while ago I wrote an article on how to use mod_rewrite for search engine friendly URLs, but have since found the uses lacking. For one thing, you can’t pass variables well without adding more commands to mod_rewrite. So here you’ll learn to add unlimited parameters to your links with only one simple PHP function.

Introduction

Mod_rewrite is a useful little module. In my previous article you learned how to begin to rewrite URLs to different places, but the possibilities are very limited. Unfortunately, GET variables did not pass well with mod_rewrite. You would end up with ugly URLs looking like http://yoursite.com/page&id=3. If you defined different rules in the .htaccess file, you got some limited extendability, but you still had issues with adding new variables on the fly. So here we remedy this.

Refresher And The Idea Behind It

What is the plan. Well, first, how about a quick refresh about how mod_rewrite works with an example.


RewriteEngine On RewriteRule tutorials/(.*).html index.php?id=$1 

Here’s the basic code for mod_rewrite in your .htaccess file.

RewriteEngine On defines the mod_rewrite engine on. Pretty obvious.

RewriteRule is where the magic kicks in. It basically takes two “arguments” separated by a space. The first is the pattern to match, and the second is where to rewrite it to. In this example, the pattern the rule looks for is URLs like http://yoursite.com/tutorials/How-to-use-php.html and it’ll rewrite it to http://yoursite.com/index.php?id=How-to-use-php. The magic for the dynamic-ness comes in the pattern (.*). This means to look for anything. A wildcard basically. Then in the second argument, the wildcard is used in the variable $1. You can define a few wildcards and use them like $2 for the second, $3 for the third, and so on. There is also a way to define them to only look for numbers, or for a certain amount of characters, but I’ll save that for a later date.

Now that you know basically how to use them, you can get onto the concept.

The idea is to have a rule to match anything just like the above example. This variable will be passed to a PHP file in a GET variable (like above) and will be processed by a function in PHP that will define an array with all the variables in the URL nicely processed for you. Isn’t that great?

Starting With The Rewrite Rules

Say Rewrite Rules 5 times fast. Anyways, yeah, this is your basic .htaccess file. Put it in the directory that you want to have the rules applied to.


RewriteEngine On RewriteRule index/(.*) index.php?parameters=$1 

That’s the whole file. That’s basically it. The first command turns the engine on, and the second sets the rewrite rule. Like I said, it’s pretty simple. For now, we’ll define one for index. Now if you navigate to http://yoursite.net/index/anything, it’ll be rewritten to index.php?parameters=anything. So far so good. That’s basically all you need for this.

The PHP Function

The PHP function is not hard at all.

Now we haven’t really decided how to define each variable in the URL. My idea is to have the variable name to the variable value defined in the URL and separated by slashes ”/”. So the variables would be defined as x1/x1value/x2/x2value. Knowing this, you should be able to form a very easy and great working function.

Save as croute.php

<?php $params = array(); function Router(){ global $params; $explode = explode("/", 

A while ago I wrote an article on how to use mod_rewrite for search engine friendly URLs, but have since found the uses lacking. For one thing, you can’t pass variables well without adding more commands to mod_rewrite. So here you’ll learn to add unlimited parameters to your links with only one simple PHP function.

Introduction

Mod_rewrite is a useful little module. In my previous article you learned how to begin to rewrite URLs to different places, but the possibilities are very limited. Unfortunately, GET variables did not pass well with mod_rewrite. You would end up with ugly URLs looking like http://yoursite.com/page&id=3. If you defined different rules in the .htaccess file, you got some limited extendability, but you still had issues with adding new variables on the fly. So here we remedy this.

Refresher And The Idea Behind It

What is the plan. Well, first, how about a quick refresh about how mod_rewrite works with an example.


RewriteEngine On RewriteRule tutorials/(.*).html index.php?id=$1 

Here’s the basic code for mod_rewrite in your .htaccess file.

RewriteEngine On defines the mod_rewrite engine on. Pretty obvious.

RewriteRule is where the magic kicks in. It basically takes two “arguments” separated by a space. The first is the pattern to match, and the second is where to rewrite it to. In this example, the pattern the rule looks for is URLs like http://yoursite.com/tutorials/How-to-use-php.html and it’ll rewrite it to http://yoursite.com/index.php?id=How-to-use-php. The magic for the dynamic-ness comes in the pattern (.*). This means to look for anything. A wildcard basically. Then in the second argument, the wildcard is used in the variable $1. You can define a few wildcards and use them like $2 for the second, $3 for the third, and so on. There is also a way to define them to only look for numbers, or for a certain amount of characters, but I’ll save that for a later date.

Now that you know basically how to use them, you can get onto the concept.

The idea is to have a rule to match anything just like the above example. This variable will be passed to a PHP file in a GET variable (like above) and will be processed by a function in PHP that will define an array with all the variables in the URL nicely processed for you. Isn’t that great?

Starting With The Rewrite Rules

Say Rewrite Rules 5 times fast. Anyways, yeah, this is your basic .htaccess file. Put it in the directory that you want to have the rules applied to.


RewriteEngine On RewriteRule index/(.*) index.php?parameters=$1 

That’s the whole file. That’s basically it. The first command turns the engine on, and the second sets the rewrite rule. Like I said, it’s pretty simple. For now, we’ll define one for index. Now if you navigate to http://yoursite.net/index/anything, it’ll be rewritten to index.php?parameters=anything. So far so good. That’s basically all you need for this.

The PHP Function

The PHP function is not hard at all.

Now we haven’t really decided how to define each variable in the URL. My idea is to have the variable name to the variable value defined in the URL and separated by slashes ”/”. So the variables would be defined as x1/x1value/x2/x2value. Knowing this, you should be able to form a very easy and great working function.

Save as croute.php
___FCKpd___2

So far the file is very easy, and it won’t get much harder. In the file, the variable $params is defined as an empty array to hold all the future variables. Then, inside the function Router, the $params variable is declared global so that the function can access it and it’ll reflect the changes outside the function. The only thing left to do now is to split the parameters variable up at each slash. The main for loop is also defined. The variable $i is started at 0, it loops while $i is less than the amount of entries in $explode, and after each loop $i increases by one.

This is a good start for an outline, but now you need the meat. Luckily, it’s only two lines.


<?php $params = array(); function Router(){ global $params; $explode = explode("/", 

A while ago I wrote an article on how to use mod_rewrite for search engine friendly URLs, but have since found the uses lacking. For one thing, you can’t pass variables well without adding more commands to mod_rewrite. So here you’ll learn to add unlimited parameters to your links with only one simple PHP function.

Introduction

Mod_rewrite is a useful little module. In my previous article you learned how to begin to rewrite URLs to different places, but the possibilities are very limited. Unfortunately, GET variables did not pass well with mod_rewrite. You would end up with ugly URLs looking like http://yoursite.com/page&id=3. If you defined different rules in the .htaccess file, you got some limited extendability, but you still had issues with adding new variables on the fly. So here we remedy this.

Refresher And The Idea Behind It

What is the plan. Well, first, how about a quick refresh about how mod_rewrite works with an example.


RewriteEngine On RewriteRule tutorials/(.*).html index.php?id=$1 

Here’s the basic code for mod_rewrite in your .htaccess file.

RewriteEngine On defines the mod_rewrite engine on. Pretty obvious.

RewriteRule is where the magic kicks in. It basically takes two “arguments” separated by a space. The first is the pattern to match, and the second is where to rewrite it to. In this example, the pattern the rule looks for is URLs like http://yoursite.com/tutorials/How-to-use-php.html and it’ll rewrite it to http://yoursite.com/index.php?id=How-to-use-php. The magic for the dynamic-ness comes in the pattern (.*). This means to look for anything. A wildcard basically. Then in the second argument, the wildcard is used in the variable $1. You can define a few wildcards and use them like $2 for the second, $3 for the third, and so on. There is also a way to define them to only look for numbers, or for a certain amount of characters, but I’ll save that for a later date.

Now that you know basically how to use them, you can get onto the concept.

The idea is to have a rule to match anything just like the above example. This variable will be passed to a PHP file in a GET variable (like above) and will be processed by a function in PHP that will define an array with all the variables in the URL nicely processed for you. Isn’t that great?

Starting With The Rewrite Rules

Say Rewrite Rules 5 times fast. Anyways, yeah, this is your basic .htaccess file. Put it in the directory that you want to have the rules applied to.


RewriteEngine On RewriteRule index/(.*) index.php?parameters=$1 

That’s the whole file. That’s basically it. The first command turns the engine on, and the second sets the rewrite rule. Like I said, it’s pretty simple. For now, we’ll define one for index. Now if you navigate to http://yoursite.net/index/anything, it’ll be rewritten to index.php?parameters=anything. So far so good. That’s basically all you need for this.

The PHP Function

The PHP function is not hard at all.

Now we haven’t really decided how to define each variable in the URL. My idea is to have the variable name to the variable value defined in the URL and separated by slashes ”/”. So the variables would be defined as x1/x1value/x2/x2value. Knowing this, you should be able to form a very easy and great working function.

Save as croute.php

<?php $params = array(); function Router(){ global $params; $explode = explode("/", 

A while ago I wrote an article on how to use mod_rewrite for search engine friendly URLs, but have since found the uses lacking. For one thing, you can’t pass variables well without adding more commands to mod_rewrite. So here you’ll learn to add unlimited parameters to your links with only one simple PHP function.

Introduction

Mod_rewrite is a useful little module. In my previous article you learned how to begin to rewrite URLs to different places, but the possibilities are very limited. Unfortunately, GET variables did not pass well with mod_rewrite. You would end up with ugly URLs looking like http://yoursite.com/page&id=3. If you defined different rules in the .htaccess file, you got some limited extendability, but you still had issues with adding new variables on the fly. So here we remedy this.

Refresher And The Idea Behind It

What is the plan. Well, first, how about a quick refresh about how mod_rewrite works with an example.


RewriteEngine On RewriteRule tutorials/(.*).html index.php?id=$1 

Here’s the basic code for mod_rewrite in your .htaccess file.

RewriteEngine On defines the mod_rewrite engine on. Pretty obvious.

RewriteRule is where the magic kicks in. It basically takes two “arguments” separated by a space. The first is the pattern to match, and the second is where to rewrite it to. In this example, the pattern the rule looks for is URLs like http://yoursite.com/tutorials/How-to-use-php.html and it’ll rewrite it to http://yoursite.com/index.php?id=How-to-use-php. The magic for the dynamic-ness comes in the pattern (.*). This means to look for anything. A wildcard basically. Then in the second argument, the wildcard is used in the variable $1. You can define a few wildcards and use them like $2 for the second, $3 for the third, and so on. There is also a way to define them to only look for numbers, or for a certain amount of characters, but I’ll save that for a later date.

Now that you know basically how to use them, you can get onto the concept.

The idea is to have a rule to match anything just like the above example. This variable will be passed to a PHP file in a GET variable (like above) and will be processed by a function in PHP that will define an array with all the variables in the URL nicely processed for you. Isn’t that great?

Starting With The Rewrite Rules

Say Rewrite Rules 5 times fast. Anyways, yeah, this is your basic .htaccess file. Put it in the directory that you want to have the rules applied to.


RewriteEngine On RewriteRule index/(.*) index.php?parameters=$1 

That’s the whole file. That’s basically it. The first command turns the engine on, and the second sets the rewrite rule. Like I said, it’s pretty simple. For now, we’ll define one for index. Now if you navigate to http://yoursite.net/index/anything, it’ll be rewritten to index.php?parameters=anything. So far so good. That’s basically all you need for this.

The PHP Function

The PHP function is not hard at all.

Now we haven’t really decided how to define each variable in the URL. My idea is to have the variable name to the variable value defined in the URL and separated by slashes ”/”. So the variables would be defined as x1/x1value/x2/x2value. Knowing this, you should be able to form a very easy and great working function.

Save as croute.php
___FCKpd___2

So far the file is very easy, and it won’t get much harder. In the file, the variable $params is defined as an empty array to hold all the future variables. Then, inside the function Router, the $params variable is declared global so that the function can access it and it’ll reflect the changes outside the function. The only thing left to do now is to split the parameters variable up at each slash. The main for loop is also defined. The variable $i is started at 0, it loops while $i is less than the amount of entries in $explode, and after each loop $i increases by one.

This is a good start for an outline, but now you need the meat. Luckily, it’s only two lines.

___FCKpd___3

There is your Router function in its entirety. The only things added were the code inside the for loop and the call to the function at the end.

The first line inside Router adds the first variable from $explode as the key in the params array and sets it equal to the next entry which is the value. Example: x1(key, 0)/x2(value, 1). Then $i is increased so that the next loop will start with the next key, or 2 for the next loop.

Then it’s just a matter of calling the function at the end just to reduce the code for including the file.

Putting It Into Practice

Now all you need to do is put the function into practice. So start with an HTML page and include the file.

Save as index.php

<?php include("croute.php")?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Let's View An Article!</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> Body Goes here. </body> </html> 

That’s the basic page. Just including croute.php (the file with the function in it) will automatically do everything that’s needed. The array is now set, you can use it in this page. So why not start by seeing every variable in the array?


<?php foreach($params as $x1 => $x2){ echo $x1.": ".$x2."<br />"; } ?> 

That’s an easy loop. For each entry in $params as the variable $x1 to $x2 (or key to value, whatever) is then echoed out with a line break in between. This will very easily show each variable in the URL.

Testing

So now you can test it out! Upload the files to your server and navigate to the page something like this: http://old.shadow-fox.net/routing1/index/id/10/type/month.

As you can see, there are two variables, both with values. The first is id with a value of 10, and the second is type with the value of month, and that’s reflected in the page. Great, huh?

Taking It The Extra Mile

Now, having dynamic variables is great and everything, but for each page you’ll need to define a new rule in the .htaccess file, which can be very tedious. So why don’t you fix that now with a simple modification to the rewrite rule?


RewriteEngine On RewriteRule (.*)//(.*) $1.php?parameters=$2 

This is a bit different than the other one. Just adding in another wildcard into the rule and variable wouldn’t work, because the two variables are already separated by a slash (/). This will mess up the rules and add to the problems. So instead, if the first variable is separated by two slashes, then adding just one slash to separate the variables should be no problem right? Well if you test it, you’ll see that it is right.

Conclusion

So there you go. Adding dynamic pages and dynamic variables for dynamic URLs is not difficult at all, and now you can use this router function on all your pages for great looking memorable addresses and an easy way to organize your pages.

And here’s your finished page.

  GET["parameters"]); for($i=0; $i < count($explode); $i++){ } } ?>

So far the file is very easy, and it won’t get much harder. In the file, the variable $params is defined as an empty array to hold all the future variables. Then, inside the function Router, the $params variable is declared global so that the function can access it and it’ll reflect the changes outside the function. The only thing left to do now is to split the parameters variable up at each slash. The main for loop is also defined. The variable $i is started at 0, it loops while $i is less than the amount of entries in $explode, and after each loop $i increases by one.

This is a good start for an outline, but now you need the meat. Luckily, it’s only two lines.

___FCKpd___3

There is your Router function in its entirety. The only things added were the code inside the for loop and the call to the function at the end.

The first line inside Router adds the first variable from $explode as the key in the params array and sets it equal to the next entry which is the value. Example: x1(key, 0)/x2(value, 1). Then $i is increased so that the next loop will start with the next key, or 2 for the next loop.

Then it’s just a matter of calling the function at the end just to reduce the code for including the file.

Putting It Into Practice

Now all you need to do is put the function into practice. So start with an HTML page and include the file.

Save as index.php
___FCKpd___4

That’s the basic page. Just including croute.php (the file with the function in it) will automatically do everything that’s needed. The array is now set, you can use it in this page. So why not start by seeing every variable in the array?

___FCKpd___5

That’s an easy loop. For each entry in $params as the variable $x1 to $x2 (or key to value, whatever) is then echoed out with a line break in between. This will very easily show each variable in the URL.

Testing

So now you can test it out! Upload the files to your server and navigate to the page something like this: http://old.shadow-fox.net/routing1/index/id/10/type/month.

As you can see, there are two variables, both with values. The first is id with a value of 10, and the second is type with the value of month, and that’s reflected in the page. Great, huh?

Taking It The Extra Mile

Now, having dynamic variables is great and everything, but for each page you’ll need to define a new rule in the .htaccess file, which can be very tedious. So why don’t you fix that now with a simple modification to the rewrite rule?

___FCKpd___6

This is a bit different than the other one. Just adding in another wildcard into the rule and variable wouldn’t work, because the two variables are already separated by a slash (/). This will mess up the rules and add to the problems. So instead, if the first variable is separated by two slashes, then adding just one slash to separate the variables should be no problem right? Well if you test it, you’ll see that it is right.

Conclusion

So there you go. Adding dynamic pages and dynamic variables for dynamic URLs is not difficult at all, and now you can use this router function on all your pages for great looking memorable addresses and an easy way to organize your pages.

And here’s your finished page.

  GET["parameters"]); for($i=0; $i < count($explode); $i++){ $params[$explode[$i]] = $explode[$i+1]; $i++; } } Router(); ?>

There is your Router function in its entirety. The only things added were the code inside the for loop and the call to the function at the end.

The first line inside Router adds the first variable from $explode as the key in the params array and sets it equal to the next entry which is the value. Example: x1(key, 0)/x2(value, 1). Then $i is increased so that the next loop will start with the next key, or 2 for the next loop.

Then it’s just a matter of calling the function at the end just to reduce the code for including the file.

Putting It Into Practice

Now all you need to do is put the function into practice. So start with an HTML page and include the file.

Save as index.php
___FCKpd___4

That’s the basic page. Just including croute.php (the file with the function in it) will automatically do everything that’s needed. The array is now set, you can use it in this page. So why not start by seeing every variable in the array?

___FCKpd___5

That’s an easy loop. For each entry in $params as the variable $x1 to $x2 (or key to value, whatever) is then echoed out with a line break in between. This will very easily show each variable in the URL.

Testing

So now you can test it out! Upload the files to your server and navigate to the page something like this: http://old.shadow-fox.net/routing1/index/id/10/type/month.

As you can see, there are two variables, both with values. The first is id with a value of 10, and the second is type with the value of month, and that’s reflected in the page. Great, huh?

Taking It The Extra Mile

Now, having dynamic variables is great and everything, but for each page you’ll need to define a new rule in the .htaccess file, which can be very tedious. So why don’t you fix that now with a simple modification to the rewrite rule?

___FCKpd___6

This is a bit different than the other one. Just adding in another wildcard into the rule and variable wouldn’t work, because the two variables are already separated by a slash (/). This will mess up the rules and add to the problems. So instead, if the first variable is separated by two slashes, then adding just one slash to separate the variables should be no problem right? Well if you test it, you’ll see that it is right.

Conclusion

So there you go. Adding dynamic pages and dynamic variables for dynamic URLs is not difficult at all, and now you can use this router function on all your pages for great looking memorable addresses and an easy way to organize your pages.

And here’s your finished page.

  GET["parameters"]); for($i=0; $i < count($explode); $i++){ } } ?>

So far the file is very easy, and it won’t get much harder. In the file, the variable $params is defined as an empty array to hold all the future variables. Then, inside the function Router, the $params variable is declared global so that the function can access it and it’ll reflect the changes outside the function. The only thing left to do now is to split the parameters variable up at each slash. The main for loop is also defined. The variable $i is started at 0, it loops while $i is less than the amount of entries in $explode, and after each loop $i increases by one.

This is a good start for an outline, but now you need the meat. Luckily, it’s only two lines.

___FCKpd___3

There is your Router function in its entirety. The only things added were the code inside the for loop and the call to the function at the end.

The first line inside Router adds the first variable from $explode as the key in the params array and sets it equal to the next entry which is the value. Example: x1(key, 0)/x2(value, 1). Then $i is increased so that the next loop will start with the next key, or 2 for the next loop.

Then it’s just a matter of calling the function at the end just to reduce the code for including the file.

Putting It Into Practice

Now all you need to do is put the function into practice. So start with an HTML page and include the file.

Save as index.php
___FCKpd___4

That’s the basic page. Just including croute.php (the file with the function in it) will automatically do everything that’s needed. The array is now set, you can use it in this page. So why not start by seeing every variable in the array?

___FCKpd___5

That’s an easy loop. For each entry in $params as the variable $x1 to $x2 (or key to value, whatever) is then echoed out with a line break in between. This will very easily show each variable in the URL.

Testing

So now you can test it out! Upload the files to your server and navigate to the page something like this: http://old.shadow-fox.net/routing1/index/id/10/type/month.

As you can see, there are two variables, both with values. The first is id with a value of 10, and the second is type with the value of month, and that’s reflected in the page. Great, huh?

Taking It The Extra Mile

Now, having dynamic variables is great and everything, but for each page you’ll need to define a new rule in the .htaccess file, which can be very tedious. So why don’t you fix that now with a simple modification to the rewrite rule?

___FCKpd___6

This is a bit different than the other one. Just adding in another wildcard into the rule and variable wouldn’t work, because the two variables are already separated by a slash (/). This will mess up the rules and add to the problems. So instead, if the first variable is separated by two slashes, then adding just one slash to separate the variables should be no problem right? Well if you test it, you’ll see that it is right.

Conclusion

So there you go. Adding dynamic pages and dynamic variables for dynamic URLs is not difficult at all, and now you can use this router function on all your pages for great looking memorable addresses and an easy way to organize your pages.

And here’s your finished page.

 
  相关解决方案