Profile photo
Submitted by Tim Joseph
on June 14th, 2011 at 13:04:28 EST

How to display my Url in REST style with php?

I have a php application where I have dynamic page like category.php?c=1. I mean my pages are dynamic based on parameters. Now, I want to have more meaningful shortcut url like
How do I have just meaningful url. These days, its important to have meaningful url for Search engine optimization (SEO). I don't really want REST protocol but want url in REST style.

Profile photo
Replied by Tim Joseph
on June 16th, 2011 at 09:25:30 EST
Any suggestion please???
Edited on June 16th, 2011 at 09:26:18 EST

Profile photo
Replied by RaKumar
on June 16th, 2011 at 09:27:04 EST
If your web hosting server supports Apache mod_rewrite feature, you can easily achieve what you are looking for using mod_rewrite. This is server level redirection by Apache server.
All you need to do is write following code in your .htaccess file in your website root.
RewriteEngine on 
RewriteRule /category/([\d]+)/[\w]* /category.php?c=$1

It is all based on regular expression. In the sample code above, the regular expression match under parenthesis ([\d]+) will be passed as parameter $1 to actual page parameter.

For more detail on mod_rewrite,
Accepted Answer

Profile photo
Replied by David Wu
on June 16th, 2011 at 18:30:33 EST
Another way for pretty url is using Apache module "ForceType". First need to tell the web server to treat file name without extension "category" as php script. However, your web hosting need to support using ForceType module.

Write code below your .htaccess file in your website root location.
<Files category>
ForceType application/x-httpd-php

Now, the Apache web server will treat file name "category" as php script.

Then, create the file without extension named "category".

Now the url to access this category page is: -> corresponds to php page category without extension.
Now you can also pass url like and still get resolved to "category" page. What happens here is when Apache server look for file path "/category/1/levis-jeans" which of course does not exists and it won't be able to find it. So, Apache server will look for one level down "category/1" and so on. Finally, it will "category" file. So, now all your requests will be going to one single "category" page.

Now in your "category" page, which is php page, you can write your code to parse the url, get the required parameters and do whatever you need to do.
For example:
$urlParts = explode("/", $url);
if REQUEST_URI servervariable returns "/test/test1" then
$urlParts[0] is always empty
$urlPart[1] contains test
$urlPart[2] contains test1

More details:
Edited on June 16th, 2011 at 18:35:35 EST

Profile photo
Replied by Tim Joseph
on June 16th, 2011 at 22:48:12 EST
Thanks everything. mod_rewrite - RewriteRule actually resolved my issue. It was simple solution.