SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Should this be php script or controlled with .htaccess file ?

    Hi,

    I am directing users from here:
    url : mypage.example.com

    to here : example.com/mypage/

    BUT if the directory "mypage" does not exist,
    then I want to direct the user to

    to: example.com/info.php?pg=mypage

    ( Obviously "mypage" has many variations )

    I don't think I can put if statements in the .htaccess file

    but how would I do this in php script ?

    Any suggestions on how to do this ?

    Thanks.


    .

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,031
    Mentioned
    152 Post(s)
    Tagged
    2 Thread(s)
    You can actually do this in .htaccess! (and you should, because it's faster than doing it in PHP)

    Code:
    Options +FollowSymlinks -MultiViews
    
    RewriteCond %{HTTP_HOST} ^(.*)\.example\.com$ [NC]
    RewriteCond %1 -d
    RewriteRule .? http://example.com/%1/ [L,R=302]
    
    RewriteCond %{HTTP_HOST} ^(.*)\.example\.com$ [NC]
    RewriteRule .? http://example.com/info.php?pg=mypage [L,R=302]
    Once you're happy with how it works replace R=302 by R=301
    Make sure you handle www. before you do any of this, otherwise you'll be rewriting www as well (you can add [samp]RewriteCond %1 !www as a second RewriteCond to both blocks to prevent www from being handled by these blocks).
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,644
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Ah, actually, I don't think that will work because subdomains are actually separate domains. Translated, you need to declare your subdomain names in order for the host's DNS to send the request to your domain.

    If the host has such a trick up their sleeve, then Rémon's code should work (if the *'s are replaced with +'s, and the last line's mypage replaced with %1 [the atom generated by the EVERYTHING atom in the attached RewriteCond statement]).

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  4. #4
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,031
    Mentioned
    152 Post(s)
    Tagged
    2 Thread(s)
    The easiest way to do this is with wildcard DNS. Most respectable DNS daemons support this.
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  5. #5
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,644
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Rémon,

    Bingo! That would do it on the DNS side. With the mods to your mod_rewrite code, jekko should be in business.

    Regards

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator


Tags for this Thread

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
  •