SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    SitePoint Evangelist
    Join Date
    Nov 2005
    Posts
    496
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How do I setup mydomain.co.uk/username/ folders?

    Hello everyone. Hope you're all well (and hope someone can help me here!)

    I'm working on a project that needs to allow people to type in mydomain.co.uk/username/ as a URL and then this will redirect them to their profile page. I'm using PHP to code the site, so a PHP related solution would be great.

    My project is working around profile pages. These profile pages will have stuff on it, related to that username. I was thinking of when a user signs up, my PHP code creates a new folder on the root directory named with the username's username... Then the PHP code will copy a template profile page into this newly created directory...

    Would this be a good way of doing it? It'd be nice if these profile pages were search engine friendly too...

    Any help at all would be great!

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,509
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Look at this thread, and Dachande663 's tip.

  3. #3
    SitePoint Evangelist
    Join Date
    Nov 2005
    Posts
    496
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ahh, sorry should of done more detailed search before posting...

    This mod_rewrite melarky... How does this actually work then? What does it actually do? Also, will using that be search engine friendly? I kind of want these profile pages to be a page of their own (in search engine terms)...

  4. #4
    SitePoint Zealot Dachande663's Avatar
    Join Date
    Feb 2005
    Location
    Birmingham, UK
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In the root of the website you would make a .htaccess file (take care when naming it, the htaccess part is the file extension, there is no filename). Within that file put this:

    Code:
    RewriteEngine on
    RewriteRule ^(.*)$ index.php?username=$1
    Then, in the index.php file you can just use $_GET['username'] to select the relevant userdata (don't forget to sanitise it though).

    The only downside to a system like this is, if you have say /about or /contact the script will try and load users with that name. You can stop that by modyfying your htaccess file like so:

    Code:
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?username=$1
    Now apache will check any requests to see if a file or directory with that name already exists and if so load that instead. And yes, this system is very search engine friendly because as far as the search engine is concerned these are separate pages.

  5. #5
    SitePoint Evangelist
    Join Date
    Nov 2005
    Posts
    496
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Excellent. Thank you very much Dachande663 - that was a great response!

    So... With the code you provided... That should do the job right? I will probably use the page "profile.php" and pass "id" to it... So my code in the .htaccess file will be...

    Code:
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ profile.php?id=$1
    Is that correct right? Then on the profile.php I will have $_REQUEST['id']? To validate the ID, I need to check that it matches a username in the database yes? Is there anyother checks I need to do it?

    Also, Is there any special requirements for this thing to work? Will I need to contact my hosting company to make sure it supports this type of thing?

    Many many thanks again...

  6. #6
    SitePoint Evangelist
    Join Date
    Nov 2005
    Posts
    496
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just an addtional note... The $1 at the end of that above code... Why is it $1? Where does that 1 come from?

  7. #7
    SitePoint Zealot Dachande663's Avatar
    Join Date
    Feb 2005
    Location
    Birmingham, UK
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    littlejim, the $1 comes from the way apache handles mod_rewrite, it basically means the first variable. So if you had two regex's you would then have $1 and $2. Read up on it if you want, you can do amazing things with a dash of m_r.

    As for the PHP page, I would normally use $_GET['id'] but request will work. Just remember to sanitise the input in case someone tries an SQL injection attack. This code should help get you started:

    PHP Code:
    <?php
    // connect to database here
    $username mysql_real_escape_string($_GET['id']);
    // now you can perform a query to get the users data etc
    ?>

  8. #8
    SitePoint Evangelist
    Join Date
    Nov 2005
    Posts
    496
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow... Thanks man... You've been really really helpful. Thanks for steering me in the right direction - I now have a firm grasp and will continue to look up on it.

    Thanks again!

  9. #9
    SitePoint Zealot Dachande663's Avatar
    Join Date
    Feb 2005
    Location
    Birmingham, UK
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah you're welcome, just glad I could spread the magic of mod_rewrite.

  10. #10
    SitePoint Member
    Join Date
    Nov 2005
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just been reading this post and its very very interesting, so doing this in your .htaccess

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ profile.php?id=$1

    and then a person types yourdomain.com/yourname it will basicly run yourdomain.com/profile.php?id=yourname with out them knowing so it looks as though your just going to yourdomain.com/yourname?

    Many thanks

  11. #11
    SitePoint Evangelist
    Join Date
    Feb 2005
    Posts
    581
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, unless you throw in a redirect ([R]) into the mix.
    I will not flame the newbies,
    I will not flame the newbies,
    I will flame the newbies...
    Table free is the way to be!

  12. #12
    SitePoint Member
    Join Date
    Nov 2005
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What do you mean by

    Yes, unless you throw in a redirect ([R]) into the mix.
    ?

  13. #13
    SitePoint Zealot Dachande663's Avatar
    Join Date
    Feb 2005
    Location
    Birmingham, UK
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you add an R flag to the rule the browser will recieve a Redirect code in the header, showing that the page has moved. However this harms your search results as it's merely counted a re-direct page, not a full page in itself.
    Web Developer & Geek: hybridlogic.co.uk ~ lukelanchester.com

  14. #14
    SitePoint Evangelist
    Join Date
    Feb 2005
    Posts
    581
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    RewriteRule ^(.*)$ profile.php?id=$1 [R]
    As daschande63 says, not a good idea.
    I will not flame the newbies,
    I will not flame the newbies,
    I will flame the newbies...
    Table free is the way to be!

  15. #15
    SitePoint Member
    Join Date
    Nov 2005
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I wrote the code into my .htaccess

    Code:
    RewriteEngine on
    RewriteCond &#37;{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ profile.php?id=$1
    But then when i did mydomain.com/myid i got a 500 Internal Server Error.....Anyone know why this could be??

    Many thanks

  16. #16
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    in httpd.conf You should probably uncomment
    LoadModule rewrite_module modules/mod_rewrite.so
    and restart the server
    my mobile portal
    ghiris.ro


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •