Piyush
Piyush

Reputation: 1568

SEO friendly and clean URL in php

Actually my question can not understand directly but i think you can understand from following example

here in wordpress not any html or php page page or folder name gadgets is not available in server but when user visit this link open as html

i create my own website where uer login they visit there profile from www.website.com/profile.php but i want to give every user to link like www.website.com/userid like facebook


so i want to know only how to these url are open i develop whole website but i want update only /profile.php to /username

thanks for read it and answer it

Upvotes: 0

Views: 994

Answers (4)

Piyush
Piyush

Reputation: 1568

on this link i write after googling about this problem and i found solution Click here for see this tutorial then read following steps

0) Question

I try to ask you like this :

i want to open page like facebook profile www.facebook.com/kaila.piyush

it get id from url and parse it to profile.php file and return featch data from database and show user to his profile

normally when we develope any website its link look like www.website.com/profile.php?id=username example.com/weblog/index.php?y=2000&m=11&d=23&id=5678

now we update with new style not rewrite we use www.website.com/username or example.com/weblog/2000/11/23/5678 as permalink

http://example.com/profile/userid (get a profile by the ID) 
http://example.com/profile/username (get a profile by the username) 
http://example.com/myprofile (get the profile of the currently logged-in user)

1) .htaccess

Create a .htaccess file in the root folder or update the existing one :

Options +FollowSymLinks
# Turn on the RewriteEngine
RewriteEngine On
#  Rules
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php

What does that do ?

If the request is for a real directory or file (one that exists on the server), index.php isn't served, else every url is redirected to index.php.

2) index.php

Now, we want to know what action to trigger, so we need to read the URL :

In index.php :

// index.php    

// This is necessary when index.php is not in the root folder, but in some subfolder...
// We compare $requestURL and $scriptName to remove the inappropriate values
$requestURI = explode(‘/’, $_SERVER[‘REQUEST_URI’]);
$scriptName = explode(‘/’,$_SERVER[‘SCRIPT_NAME’]);

for ($i= 0; $i < sizeof($scriptName); $i++)
{
    if ($requestURI[$i] == $scriptName[$i])
    {
        unset($requestURI[$i]);
    }
}

$command = array_values($requestURI);
With the url http://example.com/profile/19837, $command would contain :

$command = array(
    [0] => 'profile',
    [1] => 19837,
    [2] => ,
)
Now, we have to dispatch the URLs. We add this in the index.php :

// index.php

require_once("profile.php"); // We need this file
switch($command[0])
{
    case ‘profile’ :
        // We run the profile function from the profile.php file.
        profile($command([1]);
        break;
    case ‘myprofile’ :
        // We run the myProfile function from the profile.php file.
        myProfile();
        break;
    default:
        // Wrong page ! You could also redirect to your custom 404 page.
        echo "404 Error : wrong page.";
        break;
}

2) profile.php

Now in the profile.php file, we should have something like this :

// profile.php

function profile($chars)
{
    // We check if $chars is an Integer (ie. an ID) or a String (ie. a potential username)

    if (is_int($chars)) {
        $id = $chars;
        // Do the SQL to get the $user from his ID
        // ........
    } else {
        $username = mysqli_real_escape_string($char);
        // Do the SQL to get the $user from his username
        // ...........
    }

    // Render your view with the $user variable
    // .........
}

function myProfile()
{
    // Get the currently logged-in user ID from the session :
    $id = ....

    // Run the above function :
    profile($id);
}

Upvotes: 1

Ben Harvey
Ben Harvey

Reputation: 1843

I think you are asking about clean urls?

You don't want to do this http://mysite.com/profile.php?id=foo

But you would like this http://mysite.com/foo

Where you pass the username, in this case 'foo' to some script so you can do something with it.

@godesign got it right by telling you that you need to enable mod_rewrite in your .htaccess file. There are a lot of interesting things you can do with it, and you should read up on it and regular expressions too.

This is what my .htaccess file looks like (modified for the example):

RewriteEngine On
RewriteRule ^([a-z]+)$ profile.php?id=$1 [L]

This takes whatever matches the regular expression - the ^([a-z]+)$ bit - and puts it into the $1 variable.

Read more:

http://wettone.com/code/clean-urls

http://corz.org/serv/tricks/htaccess2.php

http://www.branded3.com/blogs/htaccess-mod_rewrite-ultimate-guide/

Upvotes: 0

Peter Wooster
Peter Wooster

Reputation: 6089

If you want a WordPress site where each user has their own subsite you should look at BuddyPress. It's a WordPress social network plugin.

Upvotes: 0

Hangover
Hangover

Reputation: 51

Enable mod_rewrite, apache => htaccess

Upvotes: 1

Related Questions