Having cute URLs like Facebook

+1 Edimaobong Matthew · July 3, 2014
I want to cut my url short and make it pretty, I tried this in my htaccess file (to remove the .php)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

It works just fine, I can view my files as localhost/website/affiliates

Now, I want to tackle the variables and make it look pretty like facebook,
I want it to be like, localhost/website/2sweetworld
instead of; localhost/website/profile.php?username=2sweetworld,
I tried this on the same htaccess file;

RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?username=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ profile.php?username=$1

It kinda conflicts with the first codes hence giving me issues.
Help, please.

Post a Reply

Replies

Oldest  Newest  Rating
+1 Samuel Oloruntoba · July 3, 2014
one thing i'd suggest is that you use symfony routing, class. You can head over to symfony.com and read the documentation
0 Shamal Sandeep · August 9, 2014
I too love that kind of urls. You can achieve it by this method

For example let's imagine that you want to rewrite

http://localhost/user.php?user=Matthew
as 
http://localhost/user/Matthew

What you want to do is not to remove the .php extension But to do this


#First turn the RewriteEngine On
RewriteEngine on

#And then direct that Semantic url(In your words cute url) to the PHP file
RewriteRule ^user/([0-9a-zA-Z]+) user.php?user=$1 [NC,L]


The second line if brief:
It first request the user keyword and goes to the next portion of the url simply Matthew or James or something, It should be upper case or lower case or digits. If it matches the page is requests the php file. If it's not 404 page will be fired of!
0 Godspower Onedo · September 30, 2014
Wow Nice tip, thanks.
0 Shamal Sandeep · October 1, 2014
You're welcome!
0 Chathula Sampath · October 23, 2014
i am using .htaccess (Mod_rewrite) with PHP.. it is awesome if you can use it.. very easy to handle the urls.

you can use .htaccess like this..


Options -Indexes
<IfModule mod_rewrite.c>
    RewriteEngine On

# if path is empty leave it /
    RewriteBase /your_path/

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

# thus matches anything after index.php ex: example.com/sjxjsj or example.com/index.php/xxxx
    RewriteRule ^(.*)$ index.php/$1
</IfModule>


This is the php class that i use to get the url portion that you want...
if the url is something like example.com/xxx/xxx/xx , not a problem to me...



<?php  
/**
     * @author Chathula Sampath <Schathula@gmail.com>
     * @license http://www.php.net/license/3_01.txt PHP License 3.01
     * @link http://online.lk || http://fb.com/c.sampathperera || http://twitter.com/ChathulaC
*/

class SimpleUrl {

    public $site_path;

    /* SET THE SITE PATH */
    public function __construct($site_path) {
        $this->site_path = ($site_path[0] == "/") ? $this->removeSlash($site_path) : "/" . $this->removeSlash($site_path);
    }

    /* TO STRING MAGIC METHOD */
    public function __toString() {
        return $this->site_path;
    }

    /* REMOVE SLASH FROM SITE PATH */
    private function removeSlash($string) {
        if ($string[strlen($string) - 1] == "/") {
            $string = rtrim($string, "/");
        }

        return $string;
    }

    /* GET THE SITE SEGMENT */
    public function segment($segment) {

        $url = str_replace($this->site_path, '', $_SERVER['REQUEST_URI']);
        $url = explode("/", $url);
        if (isset($url[$segment])) {
            return $url[$segment];
        } else {
            return false;
        }

    }


}


create the object for use the class...


<?php
// leave it black if you don't have a path
$url = new SimpleUrl('your_path');


now you can use it easily... k we will say that we want to get the data where example.com/user/name you can do something like this....



<?php

if ($url->segment(1) == 'user') {
    if ($url->segment(2) == 'username') {
        echo 'user found';
    } else {
        echo 'user not found';
    }
}

very easy and powerful way to handle the clean urls...
  • 1

PHP

107,130 followers
About

Server-side, HTML embedded scripting language used to create dynamic Web pages.

Links
Moderators