Note: When I say "Digg Style urls", I am not implying that they were invented by Digg! It is just for the sake of demonstrating the type of URLs I am talking about!
The days of index.php?var1=name&var2=surname should be over.
You might have noticed that Digg has a cool way of maintaining clean URLs. Digg actually uses LAMP - Linux/Apache/MySQL/PHP. But where are the .php extensions? The answer is here. 4 steps to implement your own Digg style clean URLs. I am not saying the following steps is what Digg.com did, but they merely achieve the same effect. There are obviously other ways.
- You obviously should have Apache and PHP firing away. Option 1 has been recommended by most readers of this blog. You can try both and see what works best for you. Both options involve using the .htaccess file.
Option 1 (overwhelming recomendations by readers).
In your .htaccess file, add these lines
RewriteEngine On
RewriteRule ^(hello|index)(/.*)?$ $1.php
where hello and index are php scripts that you want to use without the .php extension. You can add more scripts in the rewrite rule by piping using more of these....|. In your filesystem, these scripts will still carry the .php extension, but you drop it in your URL.
Option2
So the first step is force Apache to accept your php file that does not have a .php extension. Suppose you have hello.php. Rename it to hello only. Now force Apache like so:
This will basically tell Apache to treat hello as a php file.
ForceType application/x-httpd-php - Put the above configuration in a .htaccess file that will reside inside the folder where php scripts will be served. Apache is now ready to serve pages without the .php extension.
- Use urls of this type: http://www.mydigg.com/hello/var1/var2.
In the url above, hello is a php script while var1 and var2 are variables being passed to it. - Inside your hello PHP script, split this url into an array, using the "/" as a separator, and handle the vars.
$urlArray = explode("/",$_SERVER["REQUEST_URI"]);
$var1_value = $urlArray[count($urlArray)-1];
$var2_value = $urlArray[count($urlArray)-2];
.....and you start doing what you know best!
- Nobody will know what language your are using on the server side. So more security
- Users can remember them more easily than something like: index.php?mya=67686&uyu=hghg88g&tyr%230ghgtu=6768
- Makes your site cool like Digg.com!
Originally seen at Truehacker.blogspot .Page not available anymore
No comments:
Post a Comment