Trying to get some values of my friendly url

Im trying to get some values that I pass in my url but Im having one problem.

Im using this code and function to get my url:

$page = $url[1];
$page = ($page ? $page : 1);
echo '<h1>'.$page.'</h1>';

And my function getHome():

function getHome()
    $url = $_GET['url'];
    $url = explode('/', $url);
    $url[0] = ($url[0] == NULL ? 'index' : $url[0]);

          require_once('tpl/'.$url[0].'.php');                (tpl is my folder, where I have my php files)

With this, I can get value that I pass in url in my “categories.php” file, using this url: htttp://localhost/project/categories/2 -> I´m getting echo of my “2” and also I have my categories.php page included.

But in my “index.php” file, using this url: htttp://localhost/project/2 -> Im not getting value that I pass in url, and its included my page “404 not found”, that I include in my function getHome like this: “tpl/404.php”.

If I use index in my URL, like this: htttp://localhost/project/index/2, it works, I can get my url value of “2”. But I´m trying to have just my htttp://localhost/project/2, without index, and get the value I pass, in this case “2”.

So, Im trying to find a way to change my function geHome(), to get values that I pass in url also in my index.php file, using: localhost/project/2 , where I want to get my value “2” in this case, and not include my ‘tpl/404.php’.

But Im not having sucess doing this change, do you see some way to do it?

Im also using my .htaccess with my rule, saying that my index.php is my default, where I acess all, where I do my navigation:

RewriteEngine OnRewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1

My project organization is:

  • 1 folder project, inside it I have:
  • 1 .htaccess file
  • 1 index.php file where I call my function getHome()
  • 1 folder “tpl” where I have my index.php, that´s my home page, and I also have my other php files

Would something like this work?


if (substr_count($url, '/') == 1) {
	echo "index.php";
} else {


Just check the number of “/”, if it’s less than 2, you know it’s a call to “index.php”.

Thanks xMog but dont works :/. And just now I noticed one thing, when I try acess localhost/project/categories/3, I can get my 3, and my categories page is included, but I have some images in this categories page and this images just appears in localhost/projcet/categories, if I pass some value to my url like localhost/project/categories/test … my page categories is included normally but my images dont appear. Do you know also why this can be happening?

For the images, I think I know. You probably have the image tags as such:
<img src=“./images/image.jpg”> (notice the dot “.” at the beginning of the URL
This “dot” means "starting from the current URL, go into the images folder and display the image.jpg image.
So, if you’re at localhost/project/categories/3, the image should be in the folder localhost/project/categories/3/images/image.jpg

If you want to keep your images at a folder like :

Then you need to write your img tags like this:
<img src=“/images/image.jpg”>
No dot, starting with a “/”. the “/” means “start at the beginning of the website”.
So, even if you’re in, if you request the image “/images/img.jpg”, it will call

It’s the same with images in CSS files.

Can you var_dump($url); after the $url = $_GET[‘url’]; line ?