1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

index.php to detect value (or mod rewrite)

Discussion in 'PHP' started by wigwambam, Sep 28, 2007.

  1. #1
    Hi,

    I'm not sure how to do this... Whether index.php can handle it or if I need to use mod rewrite? Any advice and/or examples greatly appreciated.

    I have a multi-user blog, lets say there if Fred, Peter and Jane

    I want them to have their own page, eg:
    blog.domain.com/fred
    blog.domain.com/peter
    blog.domain.com/jane

    I do NOT want to create subfolders for each user. The above should effectively be the same as:
    blog.domain.com/index.php?blog=fred
    blog.domain.com/index.php?blog=peter
    blog.domain.com/index.php?blog=jane

    Any easy way to do the shorter method?
     
    wigwambam, Sep 28, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    RewriteEngine On
    RewriteRule ^([^/]+)$ index.php?blog=$1
    
    Code (markup):
    Save this as .htaccess in your main directory.
     
    nico_swd, Sep 28, 2007 IP
  3. wigwambam

    wigwambam Peon

    Messages:
    40
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    doesn't work :(

    My index.php contains a 'coming soon' image. After I put that in my .htaccess, the index.php loads but the link to the image is broken.

    ?
     
    wigwambam, Sep 28, 2007 IP
  4. wigwambam

    wigwambam Peon

    Messages:
    40
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Sorry, forget last comment...

    I pasted this into my index.php to test:

    <?php if ($_GET["blog"]!="") {
    echo $_GET["blog"];
    } ?>

    It displays value 'index.php', not fred
     
    wigwambam, Sep 28, 2007 IP
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    nico_swd, Sep 28, 2007 IP
  6. wigwambam

    wigwambam Peon

    Messages:
    40
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Still not working :(
    If i type blog.domain.com/fred I get a 404 error. It seems its trying to load fred.html

    Thanks for your efforts btw. I'm having fun trying.
     
    wigwambam, Sep 28, 2007 IP
  7. wigwambam

    wigwambam Peon

    Messages:
    40
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I'm soooooo close!

    A the moment I have:

    RewriteEngine on
    RewriteRule ^([A-Z]+)/?$ index.php?blog=$1 [NC,QSA,L]


    If I type:
    blog.domain.com/fred
    blog.domain.com/peter

    it works fine.

    If I type:
    blog.domain.com/fred1
    blog.domain.com/peter parker

    It fails.

    I suppose I need to change the A-Z bit to inc numbers and other characters?
     
    wigwambam, Sep 28, 2007 IP
  8. wigwambam

    wigwambam Peon

    Messages:
    40
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Last post from me. Got it working, sort of.

    RewriteEngine on
    RewriteRule ^([A-Za-z0-9]+)/?$ index.php?blog=$1 [NC,QSA,L]


    It accepts usernames without spaces or symbols, ie. letters of the alphabet and number 0 to 9.

    Good enough for me. Thanks for your input nico_swd
     
    wigwambam, Sep 28, 2007 IP
  9. petronel

    petronel Peon

    Messages:
    113
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #9
    try
    
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    
    RewriteRule ^(.*)$ index.php?blog=$1 [L,QSA]
    Code (markup):
     
    petronel, Sep 28, 2007 IP