1

I am making a social networking site where I have created my own PHP mvc but I am having problems in url rewriting. My mvc works this way.

If this is the domain www.example.com/manage/posts/11111 , manage is the class, post is the method in that class & 1111 is a parameter.

The problem is that I cannot create vanity profile urls since they will not work. I want each user to have vanity profile url ie www.example.com/username but this will search for a class named username.

Kindly advise me on how

a) I can achieve vanity profile urls such as www.example.com/username without adding anything such as www.example.com/users/username.

I know there a other PHP MVCs but I just want to use my own

This is my current htaccess code::

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/?$  index.php?ref_url=$1 [NC,L,QSA]
</IfModule>
Nikolay Kostov
  • 16,433
  • 23
  • 85
  • 123

2 Answers2

0

I don't know why all people try to build their own frameworks there are so many really good frameworks that have a look at security and many more. But ok i think your problem is your rewrite rule.

RewriteRule ^(.*)$  index.php?ref_url=$1 [NC,L,QSA]

Normally you should rewrite all your input to a given file and parse the url and call the controller you need. To get a better understanding take a look at the Symfony2 Routing component.

http://symfony.com/doc/current/components/routing/introduction.html

René Höhle
  • 26,716
  • 22
  • 73
  • 82
  • I don't understand how that framework works. Is there a way you can explain how I can achieve that in a simple way. Kindly assist me. – Joseph Nyambura Apr 23 '15 at 08:53
0

You should first create a router to route your urls to controllers/classes. I will prefer klein router or FastRoute for its simplicity.

After placing a router do what u want:

(Example)

$router=new Router;

$router->addroute("/[:username]",
   function(){ 
       //Bring UserAccount Details from DB using [:username]
   }
);

$router->addroute("/manage/posts/[:id]",
   function(){
       //Get Post details using [:id]
   }

$router->dispatch();

**NB:**This is only a basic representation of usage of a router

Sreedev S B
  • 279
  • 2
  • 11