PHP hash tags URL parameters

Hi, I am not sure if this can be done with PHP but this is what I want. In Facebook when a hash tag is created the URL looks like facebook.com/hashtag/seo

I have created directory called hashtag now what should I do to create /seo.

I have an index.php in hashtag directory that checks all parameters the only thing is I don’t know to to make the URL look like that!

Lets say a user creates tag seo I want the URL look like this mysite.com/hashtag/seo instead of mysite.com/hashtag/index.php?seo

The way I’d to this is to configure the webserver to route all requests for the domain through a single file (usually index.php) and serve different content depending on the URL requested.

Here’s an example using the Slim framework to do the routing (there are plenty of other frameworks/router libraries out there which will let you do the same thing):

.htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

index.php

<?php
$app = new \Slim\Slim();
$app->get('/hashtag/:tag', function ($tag) {
    // Do something with $tag - lookup matching items etc.
});
$app->run();

If you’re not using a framework and don’t want to redirect all of your requests to the same file, you could do it yourself too.

In a .htaccess in your root folder, you can add:

RewriteEngine on
RewriteRule  ^hashtag/([a-zA-Z0-9]+)$   hashtag/index.php?hashtag=$1 [L]

This will redirect all requests to /hashtag/… to /hashtag/index.php script. The letters and numbers between the parenthesis means that it will only accept letters and numbers.

Now, to get your hashtag with PHP in your index.php file, you simply do:

 $hashtag = $_GET['hashtag'];

I’ve written a post on friendly URL on how everything works if you’re interested to know more.

1 Like

WOW what was some super fast support guys thanks big time will continue using this place to learn PHP.

Thanks gonna try your solutions.

Here is the tag form!

<form style="display:inline;" action="" method="post" name="hashtagsform">
    <input type="hidden" value="do_search" name="action">
    <input type="hidden" value="1" name="postthread">
    <input type="hidden" value="all" name="forums">
    <input type="hidden" value="posts" name="showresults">
    <input type="hidden" value="#SEO" name="keywords" class="textbox">
    <a onclick="document.hashtagsform.submit();" class="mytags_hashtag" href="" rel="nofollow">#SEO</a>
    </form>

I have index.php in /hashtags

How would you do so that I will submit similar link href=“mysite.com/hashtags/tag and that there URL will show the same.

Sorry for bad English!

I’m not sure I understand what you mean? Do you want to show the form to anybody that goes to URL like /hashtag/… ?

I am very sorry! Lets me try again.

Here is my function:

  function tag_parse($message) {
        $tweet = $message;
        return preg_replace("/#([A-Za-z0-9_]+)(?=\s|\Z)/",
            '
    <form name="hashtagsform" method="post" action="./hashtag/index.php" style="display:inline;">
    
    <input type="hidden" id="hashtagform" name="\1" />
    <a href="http://localhost/letsforum/hashtag/" class="mytags_hashtag" onclick="document.hashtagsform.submit();">#\1</a>
    </form>
            ',
            $tweet);
        return $message; 
    }

This finds hash-tags and replace them with a link! I would like the link to have URL like this: site.com/hashtag/sometag However the link seem to be canceling the form action. When I use # for the link the action works!

I want Google to follow that link and use is an anchor link!