views:

45

answers:

2

I want to create a url like www.facebook.com/username just like Facebook does it. Can we use mod_rewrite to do it. Username is name of the user in a table. It is not a sub directory. Please advise.

A: 

Sure, mod_rewrite can do that. Here is a tutorial on it.

Kevin Panko
A: 

Yes you can do this but you might have a couple of initial hurdles to get it going correctly.

The first is that you will have to use a regular expression to match it. If you don't know regex then this can be confusing at first.

The second is that you will need to take into account that of you are going to rewrite the top path on the domain you will have to have some mechanism for only rewriting if the file doesn't exist.

I guess if mod_rewrite supports testing if the url points at a real file that will be easy. If not you might have to use a blacklist of words that it wont rewrite as you will need to have some reserved words.

This would include at the least the folder that contains your images, css, js, etc and the index.php your site runs off, plus any other php files you have kicking around.

I would like to be more help but I am a .net guy and I usually help out in asp.net url rewriting issues with libraries such as UrlRewriter.net which have different configurations than mod_rewrite.

To match the username I would use a regex like this:

^/(\w*)/?$

this would then put the bit in the brackets into a variable you can use in the rewrite like

/index.php?profileName={0}

The regex I provided means:

  • ^ nothing before this
  • / forward slash
  • (\w*) any number of letters or numbers
  • /? optional forward slash
  • $ nothing after this
rtpHarry