The Lodge is members-only design/dev videos and Office Hours.

Next Office Hours Session: "Implementing an SVG Icon System" Nov 30 - 6:00 PM Eastern

ID the Body Based on URL

Last updated on:
	$url = explode('/',$_SERVER['REQUEST_URI']);
	$dir = $url[1] ? $url[1] : 'home';

<body id="<?php echo $dir ?>">

This would turn http://domain.tld/blog/home into "blog" (the second level of the URL structure). If at the root, it will return "home".

Here is an alternate method:

       $page = $_SERVER['REQUEST_URI'];
       $page = str_replace("/","",$page);
       $page = str_replace(".php","",$page);
       $page = str_replace("?s=","",$page);
       $page = $page ? $page : 'index'

This would turn http://domain.tld/blog/home into "domaintldbloghome", which is far more specific. It also will remove ".php" file extensions and the default WordPress search parameter.

More Secure Method

function curr_virtdir($echo=true){
        $url = explode('/',$_SERVER['REQUEST_URI']);
        $dir = $url[1] ? $url[1] : 'home'; // defaults to this if in the root
        $dir = htmlentities(trim(strip_tags($dir))); // prevent injection into the DOM through this function
        if ($echo) echo $dir;
        return echo $dir; // ie. curr_virtdir(false)
function get_curr_virtdir(){

Returns the "middle" directory value:

On it would return "home"
On it would return "snippets"
On it would return "forums"

The strip_tags() and htmlentities() functions prevent malicious code from being insterted into the URL and ran, e.g.

<body id="foo"><script>alert("Booo");</script>

Usage for IDing the body:

<body id="<?php curr_virtdir(); ?>">

Other usage:

  if ( get_curr_virtdir() == "store" ){
    echo "Welcome to our awesome store !";
  } elseif ( get_curr_virtdir() == "home" ){
    echo "Welcome home :-)";
  } else {
    echo "Welcome on some other page";


  1. Ant

    Hmm, I used simplier code, without throught on security. I should reconsider this.

  2. Mike
    Permalink to comment#

    This was very helpful. Im using the alternate method (second one from the top). How would I keep the id selector to the first two directories. Say I have:
    Would return id=”productsramps” and not use the third dir in the id tag?

  3. Steffen
    Permalink to comment#

    I tend to use the . This also gives a lot of usefull information, similar to theese examples:

  4. Biscutty
    Permalink to comment#

    Getting a syntax error on line6 of the secure version:

    return echo $dir; // ie. curr_virtdir(false)

    Any ideas?

  5. Biscutty
    Permalink to comment#

    Removed the “echo” – seems to have sorted it.

    function curr_virtdir($echo=true){
    $url = explode(‘/’,$_SERVER[‘REQUEST_URI’]);
    $dir = $url[1] ? $url[1] : ‘home’; // defaults to this if in the root
    $dir = htmlentities(trim(strip_tags($dir))); // prevent injection into the DOM through this function
    if ($echo) echo $dir;
    return $dir;

  6. Edmundo Junior
    Permalink to comment#

    None of this tips works for me, can you help me guys?

    My problem is, I have a site with two subdirectorys, I wanna output

    The second and third directories always change. How can the ID be just the second directory (dir2)?

    Thank you in advance!

  7. FragaGeddon
    Permalink to comment#

    Any way to get rid of the .php from the body id?

Leave a Comment

Posting Code

We highly encourage you to post problematic HTML/CSS/JavaScript over on CodePen and include the link in your post. It's much easier to see, understand, and help with when you do that.

Markdown is supported, so you can write inline code like `<div>this</div>` or multiline blocks of code in in triple backtick fences like this:

  function example() {
    element.innerHTML = "<div>code</div>";

There's a whole bunch of content on CSS-Tricks.

Search for Stuff   •   Browse the Archives

Get the Newsletter ... or get the RSS feed