This article shows how to create search engine friendly urls using PHP with or without htaccess. This simple demonstration shows how you can explode $_SERVER['REQUEST_URI'] and use the variables the same way as you would use $_GET.
If you are going to use mod_rewrite in .htaccess, then rewrite everything to the default page - in this case index.php. You can do this by inserting the following lines into .htaccess.
This rewrites everything. Then you include the following snippet to see what variables are set from
the url using print_r($_GET). To create a similar effect using the explode method you can do the following: A note about the CONSTANTS follows the code.
<?php
$url = explode('/', substr($_SERVER['REQUEST_URI'], 1) );
if ( @is_array($url) ){
$url = array_diff($url, array(''));
if (DOMAIN == 0) { $url_vars []= array_shift($url); }
if (REWRITE == 1) { $url_vars []= array_shift($url); }
if (REWRITE == 0 AND isset($url[0]) ){
if ($url[0] == basename($_SERVER['SCRIPT_FILENAME'])) {
$url_vars []= array_shift($url); }
}
}
?>
This now creates the $url array. This can be treated the same way as $_GET so you can map these variables together.
DOMAIN is a predefined variable to show whether the script is running from $_SERVER['DOCUMENT_ROOT'] or from a sub-directory (1 = domain, 0 = directory).
REWRITE is again predefined to see whether mod_rewrite is running (1 = yes, 0 = no). The value of these settings determines which values are removed from the array as they may be irrelevant to your needs.
Try using print_r($_URL) on to see what is available in the array. Write in your index.php page and try to access your page in your browser. Make sure you define the CONSTANTS.
With rewrite on: http://youdomain.com/test/page. When off: http://youdomain.com/index.php/test/page.
That's it. You may need to tweak it a bit, but in essence - fairly simple.
Comments
Thanks
Thanks for this article. I was looking for something like that for a long time..
without using mod rewite
hi,
this is my site url
http://www.designscripting.com/articles/news.php?news=6
i need to rewrite to static page without using mod rewite .....pls help me thanks in advance..
my server doesnot support mod rewrite..
Change the conditions
On you own "test" server, add the following to the bottom of news.php.
Then you can work out what the variable equating to (eg)6 would be and work from there.
<?php
$url = explode('/', substr($_SERVER['REQUEST_URI'], 1) );
if ( @is_array($url) ){
$url = array_diff($url, array(''));
}
print_r($url);
Then try the following urls like:
http://www.designscripting.com/articles/news.php/6
http://www.designscripting.com/articles/news.php
Play around with $url until you get it to be the same value as $_GET['news'] in your current script.
Br
Jamie
Unraveling the mysteries of the web
http://www.skiffie.com
Thanks
Then how to remove the .php from the url , i need url to be search engine friendly....
Thank again .. ur excellent..
Depends on your server set-up
If your server allows you to use FilesMatch in .htaccess, you can:
1) Rename your news.php to "news" - no extensions.
2) Add the following to /htaccess
<FilesMatch "^news$">ForceType application/x-httpd-php
</FilesMatch>
Br
Jamie
Unraveling the mysteries of the web
http://www.skiffie.com
Binggo
Greet article I search for this tutorial in google, its so helpful thanks
Need explanation..
Hi, I found your article is very interesting.
But my free hosting service didnt allow me to use .htaccess.
Any other way to make a "pretty" link ? I'm using Wordpress as CMS.
Thanks in advance
Use the code snippet
You could try the code snippet provided ensuring that REWRITE was off,
but I am not sure how that would work with Wordpress and the way it generates the urls...
Doesn't WP write the urls index.php/whatever anyway?
Br
Jamie
Making Drupal Mobile
not work mod_rewrite
hi
i use mod_rewrite in my htaccses and i want convert this link:
http://hgsj.com/sb/showcategory.php?cid=32 and
http://hgsj.com/sb/showcategory.php?type=2cid=32
write this rules:
RewriteRule ^showcategory/(.*)/(.*)$ showcategory.php?type=$1&id=$2 [L]
but not good work. it show a page that nopicture and no css.and add Showcategory to first other links.
please help me .why?
not work mod_rewrite
I forget.
I use - instead / and it worked.out url:
http://hgsj.com/sb/showcategory-32
http://hgsj.com/sb/showcategory-2-32
i downt now?