Reply LinkBack Thread Tools Search this Thread
Old 17-08-2005, 11:07   #1 (permalink)
stealthcow
knocking heads
 
stealthcow's Avatar
 
Join Date: Jul 2003
Location: London, UK
Posts: 1,935
PHP - virtual directories

Hi,

Just a quick overview before I get down to the nitty gritty.

I am developing a website which is membership based - i.e people can register on the site. I want each member to have their own "page". For example, let's say user "tomjones" registers with the site, I want them to be able to go to www.mysite.com/tomjones and have it display their page.

Obviously, the original way would be to do something like www.mysite.com/memberspage.php?id=tomjones

It's messy, hard for the member to remember, and not good for search engine optimisation.

I do not have root access on my host (i.e the ability to alter the Apache configuration) so that is not an option. I've been looking into htaccess modification but can't find anything that seems to match what I want to do.

In addition, what happens if I have some REAL folders? eg. www.mysite.com/stats. I wouldnt want any scripts to think "stats" was a user page..

I would appreciate any help that you can give me, whether it be a link to a tutorial or giving me a bit of advice on this forum.

Thanks in advance

Scott
  Reply With Quote
Old 17-08-2005, 11:21   #2 (permalink)
Transmogrify
Rough Creep Arse™
 
Transmogrify's Avatar
 
Join Date: Jul 2005
Location: Vancouver, Canada
Posts: 1,894
Send a message via MSN to Transmogrify
Do you know if mod_rewrite is supported on your server? If so, that'll do the job nicely.
  Reply With Quote
Old 17-08-2005, 11:25   #3 (permalink)
cam
vague™
 
cam's Avatar
 
Join Date: Mar 2004
Location: Glasgow
Posts: 5,488
aye, just use mod_rewrite.

google for 'friendly url tutorials' or something - there's loads of shit out there
  Reply With Quote
Old 17-08-2005, 11:34   #4 (permalink)
stealthcow
knocking heads
 
stealthcow's Avatar
 
Join Date: Jul 2003
Location: London, UK
Posts: 1,935
all the tutorials for mod_rewrite say that i have to have access to my appache httpd.config file - which as i mentioned before, i dont have unfortunately..

in addition, it seems that i would have to have something like www.mysite.com/file.php/variables/here

but what i need is www.mysite.com/variablehere and then a script takes variablehere (which will be the username) and then grabs that info from the database etc etc
  Reply With Quote
Old 17-08-2005, 11:39   #5 (permalink)
Transmogrify
Rough Creep Arse™
 
Transmogrify's Avatar
 
Join Date: Jul 2005
Location: Vancouver, Canada
Posts: 1,894
Send a message via MSN to Transmogrify
Take a look there.. you don't necessarily need to have .php anywhere. Also it might already be set up on your host. Drop them an email to ask if mod_rewrite has been set up, i'd be surprised if it's not.

http://www.phpfreaks.com/tutorials/23/0.php
  Reply With Quote
Old 17-08-2005, 11:51   #6 (permalink)
cam
vague™
 
cam's Avatar
 
Join Date: Mar 2004
Location: Glasgow
Posts: 5,488
it's not how we do it, but this may prove useful.

http://www.alistapart.com/articles/succeed/
  Reply With Quote
Old 17-08-2005, 11:55   #7 (permalink)
stealthcow
knocking heads
 
stealthcow's Avatar
 
Join Date: Jul 2003
Location: London, UK
Posts: 1,935
ah!

ok, thanks am awaiting the email!
  Reply With Quote
Old 17-08-2005, 13:10   #8 (permalink)
stealthcow
knocking heads
 
stealthcow's Avatar
 
Join Date: Jul 2003
Location: London, UK
Posts: 1,935
yup, its supported!

on a side note, i got a good explanation that you lot may (or may not!) find useful:

"Remember that PHP is a script parsing language. When people go to www.mysite.com/tomjones, PHP doesn't see the request at all. Apache receives the request, decides what to do with it and, if appropriate, it passes the script to PHP to be processed.

What you want to do requires modifications to be made above PHP so that Apache recognizes the "folder" as a variable to feed to PHP. I have never done this, but I have a feeling Apache's mod_rewrite might be a good place to start. However, if you don't have any access at all to Apache setup, you are S.O.L. anyways." rebelo
  Reply With Quote
Old 18-08-2005, 07:15   #9 (permalink)
stealthcow
knocking heads
 
stealthcow's Avatar
 
Join Date: Jul 2003
Location: London, UK
Posts: 1,935
ok, i need a bit more help!

mod_rewrite is clearly the way to go, thanks so far

i have the following rule in my .htaccess file
Code:
Options +FollowSymLinks RewriteEngine on RewriteRule ^(.*)/(.*) members.php?id=$1

if you go to http://developer.guavastudios.com/joebloggs/ you will see that it is working (it sets joebloggs as the id in members.php)

however, if you go to http://developer.guavastudios.com/joebloggs a 404 error is shown - it needs the trailing slash so the rule knows to put joebloggs as the id.. however, i dont want people to have to put /joebloggs/ as people, on the whole, won't remember to put a trailing slash

in the words of the beatles, "help, i need somebody!"
  Reply With Quote
Old 18-08-2005, 07:34   #10 (permalink)
stealthcow
knocking heads
 
stealthcow's Avatar
 
Join Date: Jul 2003
Location: London, UK
Posts: 1,935
just noticed one other thing.. that rule messes up directories that do exist, eg the css folder which contains my stylesheet no longer loads because it is trying to access members.php
  Reply With Quote
Old 18-08-2005, 09:00   #11 (permalink)
stealthcow
knocking heads
 
stealthcow's Avatar
 
Join Date: Jul 2003
Location: London, UK
Posts: 1,935
success...!

Code:
RewriteEngine on RewriteCond %{REQUEST_URI} !members.php RewriteRule ^(~.*) /path/to/file/members.php?id=$1 [L]

i've had to settle with having the url be www.mydomain.com/~username

but i think that will do nicely
  Reply With Quote
Old 18-08-2005, 09:04   #12 (permalink)
Brown
volkswagen yellow & gold
 
Brown's Avatar
 
Join Date: Apr 2003
Location: london, england.
Posts: 6,186
wouldn't do that if i were you. most people don't know what a tilde is, let alone how to describe what it is or where to find it on a keyboard.

google just spat this out too:

Quote:
...company just installed a new web proxy that denies access to any URL with a tilde in it. Presumably because the presence of a tilde indicates with remarkable accuracy a personal site, and apparently her employer believes that reading personal sites is not an appropriate use of company time.
  Reply With Quote
Old 23-08-2005, 08:46   #13 (permalink)
stealthcow
knocking heads
 
stealthcow's Avatar
 
Join Date: Jul 2003
Location: London, UK
Posts: 1,935
ah

any other ideas then :S
  Reply With Quote
Old 23-08-2005, 10:07   #14 (permalink)
smallbeer
I Ain't Losing Any Sleep™
 
Join Date: Apr 2003
Posts: 5,235
Could you not set a condition telling the rewrite to ignore certain directories (the physical ones).

Code:
RewriteCond $1 !^css/.* RewriteCond $1 !^images/.*
  Reply With Quote
Old 23-08-2005, 10:17   #15 (permalink)
smallbeer
I Ain't Losing Any Sleep™
 
Join Date: Apr 2003
Posts: 5,235
And the trailing slash problem.

Code:
Options +FollowSymLinks RewriteEngine on RewriteRule ^(.*)/(.*)/?$ members.php?id=$1
Code:
/?$

do you really need to use wildcards?
  Reply With Quote
Old 23-08-2005, 10:36   #16 (permalink)
stealthcow
knocking heads
 
stealthcow's Avatar
 
Join Date: Jul 2003
Location: London, UK
Posts: 1,935
cheers smallbeer

to be honest, i dont know what i "need" as i'm new to regular expresions and working with mod_rewrite (ive been browsing the net looking for solutions and just putting things together to see if they work)
  Reply With Quote
Old 23-08-2005, 11:32   #17 (permalink)
smallbeer
I Ain't Losing Any Sleep™
 
Join Date: Apr 2003
Posts: 5,235
I'm no expert either but you may want to think about how these usernames are going to be made up. just letters? alphanumeric? any other characters to be allowed? using wildcards can sometimes be dangerous as anything will be accepted.

anyway. I think I have a better solution.

Code:
RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^([A-za-z0-9]+)/?$ members.php?id=$1

it checks that teh request filename doesn't exist.

These two lines check that the request filename doesn't exist.

!-f - not a file
!-d - not a directory
  Reply With Quote
Old 23-08-2005, 11:35   #18 (permalink)
stealthcow
knocking heads
 
stealthcow's Avatar
 
Join Date: Jul 2003
Location: London, UK
Posts: 1,935
you sir, are truly the man

although, there is one slight problem with that, if someone puts in www.mysite.com/test/ (note the trailing /) it wont work..
  Reply With Quote
Old 23-08-2005, 11:39   #19 (permalink)
smallbeer
I Ain't Losing Any Sleep™
 
Join Date: Apr 2003
Posts: 5,235
what do you mean 'won't work'?

is test/ a physical directory?
  Reply With Quote
Old 23-08-2005, 11:40   #20 (permalink)
stealthcow
knocking heads
 
stealthcow's Avatar
 
Join Date: Jul 2003
Location: London, UK
Posts: 1,935
nope.. if you go to www.mysite.com/test then the graphics etc all show fine, but if you go to /test/ it calls members.php but now the images folder isnt being called correctly, and the files in root directory no longer show..
  Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Contact Us - Web Design Forums - Archive - Top
Search Engine Optimization by vBSEO 3.0.0 RC8