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.

mod_rewrite question. Paying $5.

Discussion in 'Apache' started by Xth Cannon, Apr 21, 2007.

  1. #1
    I want to use .htaccess to rewrite from "virtual" subdomains.

    Example 1
    www.soccer.domain.com
    to
    www.domain.com/index.php?k=soccer

    Example 2
    www.cancer-patients.domain.com
    to
    www.domain.com/index.php?k=cancer-patients

    However, I want the URL still displayed as the subdomain, yet it calls upon the index.php file. The tricky thing is though, I want it to do so without the subdomains actually existing. So people could enter random subdomains, (like: www.dfhjkldhfdjlkf.domain.com which would rewrite to www.domain.com/index.php?k=dfhjkldhfdjlkf).

    Is this possible?

    Thanks.
     
    Xth Cannon, Apr 21, 2007 IP
  2. Aragorn

    Aragorn Peon

    Messages:
    1,491
    Likes Received:
    72
    Best Answers:
    1
    Trophy Points:
    0
    #2
    Yes you can do that. First enable wild card subdomains for your domain then enter the following into your htaccess file.
    
    Options +FollowSymLinks
    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www\.domain.com
    RewriteCond %{HTTP_HOST} ([^.]+)\.domain.com
    RewriteRule ^(.*)$ index.php?k=%1
    
    Code (markup):
     
    Aragorn, Apr 22, 2007 IP
    weppos likes this.
  3. weppos

    weppos Well-Known Member

    Messages:
    99
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    125
    #3
    You can use the RewriteCond directive in order to check whether current host is a subdomain.

    
    RewriteCond %{REMOTE_HOST}  ^([^\.]*)\.domain.com$
    RewriteRule  ^/(.*)$ http://domain.com?k=%1  [L]
    
    Code (markup):
    I don't tested it yet but it should work.
    Here's the reference for Apache 2
    http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html

    I have to say that I don't really like this apporach.
    I would prefer to check the domain runtime in the code.
     
    weppos, Apr 22, 2007 IP
  4. Aragorn

    Aragorn Peon

    Messages:
    1,491
    Likes Received:
    72
    Best Answers:
    1
    Trophy Points:
    0
    #4
    I had tested my code. It will extract the subdomain name from www.subdomain.domain.com and subdomain.domain.com, and will rewrite the url to index.php?k=subdomain, unless the requested page is www.domain.com or domain.com.
     
    Aragorn, Apr 22, 2007 IP
  5. weppos

    weppos Well-Known Member

    Messages:
    99
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    125
    #5
    I agree your code is more complete than mine.
    We posted at the same time. :p
     
    weppos, Apr 22, 2007 IP
    Aragorn likes this.
  6. Xth Cannon

    Xth Cannon Banned

    Messages:
    352
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanks for that both of you. Aragorn, as you answered first I'll give you the $5 :p Please PM Paypal. Also, how do you set up wildcard domains?
     
    Xth Cannon, Apr 22, 2007 IP
    Aragorn likes this.
  7. Aragorn

    Aragorn Peon

    Messages:
    1,491
    Likes Received:
    72
    Best Answers:
    1
    Trophy Points:
    0
    #7
    To setup a wild cards DNS record, create a A(address) record for *.domain.com that points to your server's ip address. Most probably your control panel will be having an option for this. If not, you will have to add the line
    *.domain.com. 14400 IN A {your.web.server.ipaddress}
    Code (markup):
    to the end of your ZONE DB file. Don't forget the period after domain name.
    And then you have to edit your virtual host conf file and add the line
    ServerAlias *.domain.com
    Code (markup):
    You will be able to manually edit your virtual host settings and ZONE DB file, only if you are having a dedicated or VPS hosting. Or else you will have to contact the web hosting company to do it for you.

    The best option is to ask your web hosting company to set up the wildcard DNS for you.

    If you want to do it yourself, check
    http://allyourtech.com/content/articles/25_11_2005_setting_up_wildcard_subdomains_on_apache.php and
    http://photomatt.net/2003/10/10/wildcard-dns-and-sub-domains/
     
    Aragorn, Apr 22, 2007 IP