Supporting urls like mysite.com/username when that resource does not really exist?

Hi,

I am using php with mysql. Users of my webapp might enter a url like this into their browser:

   www.mysite.com/johndoe
   www.mysite.com/janedoe

and I want to generate a page for that user on the fly. Generating the page is no problem, but how do I get to intercept that url and handle it? Because right now the browser I guess is actually looking for a resource on the server at that path, and none will actually exist,

Thanks
DJ_AM_JuiceboxAsked:
Who is Participating?
 
UmopepisdnConnect With a Mentor Commented:
One way to do this is to use the mod_rewrite Apache module.  This will allow you to define patterns of URLs that Apache quietly reconfigures behind the scenes.

Once you have mod_rewrite setup, you could add the following into an .htaccess file:

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

The above regular expression considers the part of the URL underneath where you placed the .htaccess file (/johndoe in your case above).  If it gets all the way to the end of the string and only encounters letters or numbers, (a slash at the end is okay) it will silently reformat the URL specified to the right of the '$' character above.

This will cause Apache to process the request against the rewritten URL.  So, a request for:

http://www.mysite.com/johndoe/

to be redirected to:

http://www.mysite.com/GenerateUserPage.php?userid=johndoe

Then, you just need to have GenerateUserPage.php do its work to output an HTML page for the user.  This is a simplified example, see documentation on mod_rewrite (and regexp, for that matter) for more.

-Umo

0
 
hawardenCommented:
Are you using  www.mysite.com/index.php to generate the page? I know how to do what you are interested in via www.mysite.com/index.php?johndoe but is that what you are after?
0
 
DJ_AM_JuiceboxAuthor Commented:
Hi, sorry I wasn't specific - I'd like to do what twitter does - they allow:

   www.twitter.com/somename
   www.twitter.com/someothername

I doubt they have a folder on the file system for every user. I can use whatever method works best, I'm starting from scratch. I'm using apache with php. But the end user will literally enter one of those urls above and expect to see a page dedicated to that user,

Thanks
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
hawardenCommented:
I'm sorry, I don't know how to do that. Did a bit of digging but nothing panned out. The fact that you have to register on any of these sites implies that somewhere along the line is a database that holds these data waiting for people to access them, but how to interface with it, I'm afraid is beyond my abilities. Now you've got me interested in this and I am hoping someone else posts!
0
 
DJ_AM_JuiceboxAuthor Commented:
Yeah no prob, i know how to do it on other systems just not on apache, maybe they're using that mod_rewrite thing, or, I just have no idea what I'm talking about!
0
 
hawardenCommented:
Hmmm, how would you do it on another system (if it's not too complicated to write out)? I might be able to translate lol
0
 
DJ_AM_JuiceboxAuthor Commented:
Ok that makes perfect sense to me. For some reason, it is not able to find my genuserpage.php file. So it looks like mod_rewrite is enabled, just somehow it can't find the php file specified? Page output:

  Not Found
  The requested URL /mysite/rewrite-testfolder/genuserpage.php was not found on this server.

  Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

So my directory looks like this:

   /mysite
       /rewrite-testfolder
           .htaccess
           genuserpage.php
           index.php

and I'm just entering a url like:

   www.mysite.com/rewrite-testfolder/johndoe

and it generates the error. But it looks like the the rule is at least firing, I'm not sure why it can't find the php file because the path appears to be correct?

Thanks
0
 
UmopepisdnCommented:
What happens when you go to http://www.mysite.com/rewrite-testfolder/genuserpage.php ?
0
 
UmopepisdnCommented:
Also, what did you put in the .htaccess exactly?
0
 
DJ_AM_JuiceboxAuthor Commented:
When I go to:

   http://www.mysite.com/rewrite-testfolder/genuserpage.php

it just shows my dummy php page, which just says "hello".

These are the contents of the .htaccess file:

  RewriteEngine on
  RewriteRule ^/?([a-zA-Z0-9]+)/?$ genuserpage.php?userid=$1
0
 
UmopepisdnCommented:
Ok, so the page is found and readable.  How is the /mysite/ creeping into the URL:  It looks like it's trying (and failing) to access:

http://www.mysite.com/mysite/rewrite-testfolder/genuserpage.php
0
 
DJ_AM_JuiceboxAuthor Commented:
Hmm I see, that might be because I'm using godaddy as the host. My hosting plan (the 'unlimited' one) gives me a single domain, under which I can place several other domains, something like:

   /mycompany.com
        /companyabc.com
        /companydef.com
        /mysite

so maybe that's why it is getting messed up, the top-level mycompany/ is getting injected in there? Argh!
0
 
UmopepisdnCommented:
I have a Godaddy account.  Let me see if my plan works the same way.
0
 
UmopepisdnCommented:
Yes, my plan works the same way.  I was able to get this to work by explicitly adding the path.  There is probably a more elegant solution, but try this:

RewriteRule ^/?([a-zA-Z0-9]+)/?$ /rewrite-testfolder/genuserpage.php?userid=$1
0
 
DJ_AM_JuiceboxAuthor Commented:
Oh awesome, it's beautiful!

Ok so this works, the only issues I can think of at my novice level are making the rewrite be ignored on certain patters, like if I want to reserve:

   mysite.com/about
   mysite.com/info

then I need to modify that regular expression and I should be good to go, right?

Thanks!
0
 
UmopepisdnCommented:
In cases where the 'real' URLs are distinguishable just by the text in the URL, sure.  In cases where you need for, say, the request method (GET/POST) to be your deciding factor, you will have to dig deeper into the mod_rewrite commands you need to add to your .htaccess.

But don't do it: regexp has the property of being much easier to write than it is to read, and if you keep adding in clauses to the equation it'll quickly get hard to manage.  Keep it as simple as you can.  I suggest using another directory entirely to serve 'real' php requests from.
0
 
UmopepisdnCommented:
Actually, no.  In the cases where you'd want to add additional patterns to look for, you'd want to add additional RewriteRules to redirect to different .php files behind the scenes.
0
 
DJ_AM_JuiceboxAuthor Commented:
Ok, for example, I did this:

  RewriteEngine on
  RewriteRule ^/?(about)/?$ /rewrite-testfolder/about.php
  RewriteRule ^/?([a-zA-Z0-9]+)/?$ /rewrite-testfolder/genuserpage.php?userid=$1

and it seems to work ok. Is that reasonable?

Thanks
0
 
UmopepisdnCommented:
Yes, looks good to me :)
0
 
DJ_AM_JuiceboxAuthor Commented:
Thanks that was a huge help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.