Link to home
Start Free TrialLog in
Avatar of DJ_AM_Juicebox
DJ_AM_Juicebox

asked on

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
Avatar of hawarden
hawarden
Flag of United States of America image

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?
Avatar of DJ_AM_Juicebox
DJ_AM_Juicebox

ASKER

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
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!
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!
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
ASKER CERTIFIED SOLUTION
Avatar of Umopepisdn
Umopepisdn

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
What happens when you go to http://www.mysite.com/rewrite-testfolder/genuserpage.php ?
Also, what did you put in the .htaccess exactly?
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
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
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!
I have a Godaddy account.  Let me see if my plan works the same way.
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
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!
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.
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.
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
Yes, looks good to me :)
Thanks that was a huge help.