www.webdeveloper.com
Page 1 of 4 123 ... LastLast
Results 1 to 15 of 47

Thread: domain.com --> index.php?section=bob

  1. #1
    Join Date
    Jun 2004
    Location
    London UK
    Posts
    541

    domain.com --> index.php?section=bob

    Hi all

    Hopfully a simple one...this is what I need to do...

    redirect (or is it rewrite?)

    domain.com
    to
    domain.com/index.php?section=bob

    But keep the URL as the domain.com

    any ideas?

    Cheers
    Chris
    Successful people ask better questions, and as a result, they get better answers - Anthony Robbins

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,157
    If on Apache, I believe a .htaccess rewrite rule could handle that. Or, since it's a PHP file, you could just check to see if you got there without a URL query for "section" and set it:
    PHP Code:
    <?php
    // do this before any other processing that deals with the $_GET array:
    if(!isset($_GET['section']))
    {
      
    $_GET['section'] = 'bob';
    }
    // rest of script....
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Jun 2004
    Location
    London UK
    Posts
    541
    I need to do this with .htaccess as I have 3 URLs all pointing to the same htdocs directory.

    this is what I need in more detail:

    domain1.com ----> domain1.com/index.php?section=bob

    domain2.com ----> domain2.com/index.php?section=sue

    domain3.com ----> domain3.com/index.php?section=dan

    And I have php doing the rest of the work.

    and if posible I'd like to keep the URL as domain*.com

    Cheers
    Chris
    Successful people ask better questions, and as a result, they get better answers - Anthony Robbins

  4. #4
    Join Date
    Jun 2004
    Location
    London UK
    Posts
    541
    I need to do this with .htaccess as I have 3 URLs all pointing to the same htdocs directory.

    this is what I need in more detail:

    domain1.com ----> domain1.com/index.php?section=bob

    domain2.com ----> domain2.com/index.php?section=sue

    domain3.com ----> domain3.com/index.php?section=dan

    And I have php doing the rest of the work.

    and if posible I'd like to keep the URL as domain*.com

    Cheers
    Chris
    Successful people ask better questions, and as a result, they get better answers - Anthony Robbins

  5. #5
    Join Date
    Sep 2004
    Posts
    398
    It can still be dome ith PHP:
    PHP Code:
    <?php

    $domain 
    getenv('HTTP_HOST'); // grab the domain
    if(!isset($_GET['section']) // if $_GET['section'] isn't set
        
    && preg_match('#^(www\.)?([^\.]+)\.com$#',$domain,$match))
    // match the domain (without www. and tld)
    {
        switch(
    $match[2])
        {
            case 
    'domain1':
                
    $_GET['section'] = 'bob';
                break;
            case 
    'domain2':
                
    $_GET['section'] = 'sue';
                break;
            case 
    'domain3':
                
    $_GET['section'] = 'dan';
                break;
        }
    }
    Or with mod_rewrite:
    Code:
    Options +FollowSymLinks
    
    RewriteEngine On
    
    # domain1.com or www.domain1.com -> /index.php?section=bob
    RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.com$ [NC]
    RewriteCond %{QUERY_STRING} !section=
    RewriteRule ^(index\.php)?$ /index.php?section=bob [L]
    
    # domain2.com or www.domain2.com -> /index.php?section=sue
    RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.com$ [NC]
    RewriteCond %{QUERY_STRING} !section=
    RewriteRule ^(index\.php)?$ /index.php?section=sue [L]
    
    # domain3.com or www.domain3.com -> /index.php?section=dan
    RewriteCond %{HTTP_HOST} ^(www\.)?domain3\.com$ [NC]
    RewriteCond %{QUERY_STRING} !section=
    RewriteRule ^(index\.php)?$ /index.php?section=dan [L]
    Edit: updated mod_rewrite to add QUERY_STRING lines.
    Last edited by Scleppel; 06-23-2006 at 03:16 PM.

  6. #6
    Join Date
    Jun 2004
    Location
    London UK
    Posts
    541
    Perfect...cheers...I just forgot 1 thing:

    domain.com

    need to go to

    domain.com/index.php

    this seems to be broken now.

    domain1 2 and 3 are now working perfect

    any ideas?

    Cheers
    Successful people ask better questions, and as a result, they get better answers - Anthony Robbins

  7. #7
    Join Date
    Sep 2004
    Posts
    398
    Which one are you using?

  8. #8
    Join Date
    Jun 2004
    Location
    London UK
    Posts
    541
    sorry.....htaccess.

    Cheers
    Successful people ask better questions, and as a result, they get better answers - Anthony Robbins

  9. #9
    Join Date
    Sep 2004
    Posts
    398
    It shouldn't be causing a problem because it never matches your main domain, but you could try adding
    Code:
    # if www.domain.com or domain.com do nothing
    RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
    RewriteRule ^(index\.php)?$ - [L]
    after
    Code:
    RewriteEngine On
    and see if that helps.

  10. #10
    Join Date
    Jun 2004
    Location
    London UK
    Posts
    541
    perfect...thanks heaps

    I have one more thing I need to do...I'm hoping you can help.

    I have this URL:

    www.domain.com/user

    and it goes to:

    www.domain.com/index.php?site=user

    and also this:

    www.domain.com/user/dir

    and it goes to:

    www.domain.com/index.php?site=user&ext=dir

    and all of this doesn't mess with the earlier rewrites I did:

    i.e.
    domain.com ----> domain.com/index.php
    domain1.com ----> domain1.com/index.php?section=bob
    domain2.com ----> domain2.com/index.php?section=sue
    domain3.com ----> domain3.com/index.php?section=dan

    and the term 'user' and 'dir' can be anything

    e.g.
    www.domain.com/tom/stats
    www.domain.com/bob/images
    www.domain.com/sue/file.html

    ohh...and 'dir' can be either a directory or a file.

    thanks agin

    Cheers
    Chris
    Successful people ask better questions, and as a result, they get better answers - Anthony Robbins

  11. #11
    Join Date
    Sep 2004
    Posts
    398
    Just for domain.com? Add this to the end of your file:
    Code:
    RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
    RewriteRule ^([^/]+)/?$ /index.php?site=$1 [L]
    
    RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
    RewriteRule ^([^/]+)/(.+)$ /index.php?site=$1&ext=$2 [L]
    For all domains? Add this to the end of your file:
    Code:
    RewriteRule ^([^/]+)/?$    /index.php?site=$1        [L]
    RewriteRule ^([^/]+)/(.+)$ /index.php?site=$1&ext=$2 [L]
    You'll probably need to add
    Code:
    RewriteCond %{SCRIPT_FILENAME} -f [OR]
    RewriteCond %{SCRIPT_FILENAME} -d
    RewriteRule .* - [L]
    at the top of your file after
    Code:
    RewriteEngine On
    aswell to stop if rewriting requests for real files and directories (including index.php).

  12. #12
    Join Date
    Jun 2004
    Location
    London UK
    Posts
    541
    F*%ken perfect

    I love .htaccess

    Cheers mate...much appreciated.

    Cybercampbell
    Successful people ask better questions, and as a result, they get better answers - Anthony Robbins

  13. #13
    Join Date
    Jun 2004
    Location
    London UK
    Posts
    541
    Hi mate....just found a snag.

    if I do this: (this is just for domain.com)

    domain.com/user
    I get:
    index.php?site=user

    domain.com/user/dir
    I get:
    index.php?site=user&ext=dir

    domain.com/user/dir1/dir2/dir3
    I get:
    index.php?site=user&ext=dir1/dir2/dir3

    domain.com/user/file.html
    I get:
    index.php?site=user&ext=file.html

    domain.com/user/dir1/dir2/file.php
    I get:
    index.php?site=user&ext=dir1/dir2/file.php

    this is all perfect.....but this doesn't:

    domain.com/user/file.php?var1=one&var2=two
    I get:
    index.php?site=user&ext=file.php
    but need this:
    index.php?site=user&ext=file.php?var1=one&var2=two

    domain.com/user/dir1/dir2/file.php?var1=one&var2=two
    I get:
    index.php?site=user&ext=dir1/dir2/file.php
    but need this:
    index.php?site=user&ext=dir1/dir2/file.php?var1=one&var2=two

    any ideas?
    Successful people ask better questions, and as a result, they get better answers - Anthony Robbins

  14. #14
    Join Date
    Sep 2004
    Posts
    398
    Replace the [L] flag on these two lines
    Code:
    RewriteRule ^([^/]+)/?$ /index.php?site=$1 [L]
    # and
    RewriteRule ^([^/]+)/(.+)$ /index.php?site=$1&ext=$2 [L]
    with [QSA,L]. The QSA (Query String Append) flag will add the query string onto the end of the new URL.

  15. #15
    Join Date
    Jun 2004
    Location
    London UK
    Posts
    541
    aaaahhhh!!! didn't work

    I have this:

    Code:
    RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
    RewriteRule ^([^/]+)/?$ /usertest.php5?user=$1 [QSA,L]
    
    RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
    RewriteRule ^([^/]+)/(.+)$ /usertest.php5?user=$1&ext=$2 [QSA,L]
    have I got something wrong?
    Successful people ask better questions, and as a result, they get better answers - Anthony Robbins

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles