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.

Subdomain RewriteRule

Discussion in 'Apache' started by Lucky Bastard, Oct 19, 2005.

  1. #1
    How can I do the PROPER rewrite rule for the following:
    (*.).mydomain.com/something/(*.)/(*.) somephpfile.php?a=$1&b=$2&c=$3&%{QUERY_STRING}
     
    Lucky Bastard, Oct 19, 2005 IP
  2. johnt

    johnt Peon

    Messages:
    178
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^(^.*)\.mydomain.com$
    RewriteRule something/(^\/*)/(.*)$ somephpfile.php?a=%1&b=$1&c=$2 [QSA]
    
    Code (markup):
    cheers

    johnt
     
    johnt, Oct 20, 2005 IP
  3. Lucky Bastard

    Lucky Bastard Peon

    Messages:
    406
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks, is it possible to make the rewrite rule, rewrite for any subdomain OTHER THAN www.
    So if it is:
    www.mydomain.com/it/behaves/normal (and doesn't count the WWW as $1) (Compared with)
    harrypotter.mydomain.com/is/where/the/magic/is/at (excuse the pun)

    Hope that makes sense.
     
    Lucky Bastard, Oct 20, 2005 IP
  4. johnt

    johnt Peon

    Messages:
    178
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Yeah, you can another RewriteCond
    RewriteEngine on
    RewriteRule %{HTTP_HOST} !^www.domain.com$
    RewriteCond %{HTTP_HOST} ^(^.*)\.mydomain.com$
    RewriteRule something/(^\/*)/(.*)$ somephpfile.php?a=%1&b=$1&c=$2 [QSA]
    
    Code (markup):
     
    johnt, Oct 20, 2005 IP
  5. Lucky Bastard

    Lucky Bastard Peon

    Messages:
    406
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Doesn't work, do I need to do something on the server to allow non standard subdomains? I have a dedicated unix server.
     
    Lucky Bastard, Oct 21, 2005 IP
  6. Lucky Bastard

    Lucky Bastard Peon

    Messages:
    406
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #6
    This is what I have at the moment:
    Options -Indexes
    RewriteEngine on
    RewriteBase /
    # www rewrite rules start
    RewriteCond %{HTTP_HOST} !^www\.mydomain\.com [NC]
    RewriteRule ^(.*) http://www.mydomain.com/$1 [R=301,L]
    .... a bunch of rewrite rules here that are in relation to the www. version of mydomain.com

    # subdomain rewrite rules start
    RewriteCond %{HTTP_HOST} ^(^.*)\.mydomain\.com$
    RewriteRule product/((^\/*)/(.*))$ product.php?pid=$1&cid=$2&%{QUERY_STRING} [QSA]

    Everything worked fine until I tried to do subdomains, only thing that doesn't seem to work is the subdomain section.
    Oh yeah I am getting subdomain.mydomain.com could not found, please check the name and try again error
     
    Lucky Bastard, Oct 21, 2005 IP
  7. johnt

    johnt Peon

    Messages:
    178
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #7
    It sounds like you're trying to use dynamic subdomains - i.e. whatever.domain.com and this.domain.com and everythingelse.domain.com all resolve to your IP address.
    To do this you need to set up dynamic subdomain in DNS and in Apache. Change your BIND settings so that as well as www.domain.com resolving to your IP address, *.domain.com resolves there as well.
    In the virtual host section of your Apache config you need to add *.domain.com to the ServerAlias section

    cheers

    John
     
    johnt, Oct 21, 2005 IP
  8. Lucky Bastard

    Lucky Bastard Peon

    Messages:
    406
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I'm trying to do this, hacking my way around Apache2 without much luck
    I have found the /etc/bind/named.conf file and added (bold line) [but haven't saved it until I know what I am doing is right]:
    zone "mydomain.com" IN {
    type master;
    file "pri/mydomain.com.db";
    allow-update { none; };
    notify no;
    *.mydomain.com. 14400 IN A my.ip.address.here
    };

    Does that look right?

    Then I find /etc/apache2/httpd.conf but it contains no virtual hosts, or in fact I can't see any reference to it.

    However in my apache2, there is the following file:
    /etc/apache2/conf/vhosts/vhosts.conf which basically has a bunch of comments in it, no acual virtual hosts defined.

    Any help would be appreciated. I know nothing about this..just headbanging my way around :)
     
    Lucky Bastard, Oct 21, 2005 IP
  9. johnt

    johnt Peon

    Messages:
    178
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Not too sure about the named entry. I don't think it should go in that file. Just had a look at our config, and the file ".." entry points to a flat text file, and then we have the stuff in there, but it looks like you're using some sort of database, and I wouldn't recommend editing that ;).
    Do you have webmin or anything similar ? You can add entries for wildcards through there, I'm pretty sure that's how I did it ( its been a while ).

    As far as Apache goes, I think that you could define a virtual host in vhosts.conf. Try
    <VirtualHost *>
    ServerName domain.com
    ServerAlias *.domain.com
    DocumentRoot /path/to/my/web/files
    TransferLog /path/to/my/logfile
    </VirtualHost>
    
    Code (markup):
    and then add
    Include /etc/apache2/conf/vhosts/vhosts.conf
    Code (markup):
    into httpd.conf.

    If you can't get the DNS stuff working, you can at least test the apache side by adding something.subdomain.com to your hosts file ( on windows that's c:\windows\system32\drivers\etc\hosts ) along with the appropriate IP address.
    By adding a few subdomains into your hosts file you should be able to tell that the Apache wildcard subdomains and mod_rewrite stuff is all working, and that its only Bind that needs sorting.

    Thinking about it, you should be able to set up *.domain.com in the same way that you added domain.com and www.domain.com to your bind records.

    cheers

    John
     
    johnt, Oct 21, 2005 IP
  10. Lucky Bastard

    Lucky Bastard Peon

    Messages:
    406
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Yeah I have been trying to work out my webmin username/password just for this reason.

    Just figured out how to reset it, so now have access to webmin..first time using it so i don't know my way around :) And not sure where to do this DNS subdomain stuff ;)

    Thanks very much for the help you have provided! :)
     
    Lucky Bastard, Oct 21, 2005 IP
  11. Lucky Bastard

    Lucky Bastard Peon

    Messages:
    406
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Found these instructions posted somewhere on (I think) how to do it in webmin:
    Setup a wildcard and have [anything].yourdomain.com resolve to the IP address as well.
    1. Since you can not use wild characters on this form, simply create a record for the time being. (You will soon edit it manually).
    2. Look for the ICON titled "Name alias" and click on it.
    3. On the "Name" text box, type in "www" and enter the full domain (yourdomain.com) with a period (.) on the end and click on "create".
    4. You should see the new created alias, but now we need to change it from being a "www" to respond to all subdomains. Click on "Return to record types" to return to the "Edit Master Zone" screen.
    5. Look for "Edit Records File" and click on it to see the actual DNS record file for yourdomain.com
    6. Search and replace "www.yourdomain.com" with an asterisk (*). The record should look like this:

    * IN CNAME yourdomain.com

    Note: Do not edit anything else or you may affect the actual DNS record.
    7. Click on "Save" and then on "Return to zone list".
    8. Once you are on the BIND DNS Server page, click on "Apply changes" to active the changes.

    In about 72 hours, your dns server should respond to all queries made to your domain name.

    Do these look like the right instructions to follow to achieve the desired result?

    72 horus sounds like a long time.

    Anhything else left? Or now just getting the MOD rewrite working?
     
    Lucky Bastard, Oct 21, 2005 IP
  12. johnt

    johnt Peon

    Messages:
    178
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Yeah, that sounds about right.

    72 hours does sound like a long time, I've never known DNS changes to take that long, but if you configure your internet connection to use your name server for DNS lookup you at least you should be able to use the wildcard subdomains as you click on "Apply changes".
    After that, yes its just the mod_rewrite stuff to do.
     
    johnt, Oct 22, 2005 IP
  13. Lucky Bastard

    Lucky Bastard Peon

    Messages:
    406
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Ok, I believe my server manager has made the changes required. (DNS, and Apache).

    Now fddfd.mydomain.com resolves to the homepage.

    It would seem that I have to now get my mod rewrite rules sorted now.
    This is how it stands currently:
    Options -Indexes
    RewriteEngine on
    RewriteBase /
    RewriteCond %{HTTP_HOST} !^www\.mydomain\.com [NC]
    RewriteRule ^(.*) http://www.mydomain.com/$1 [R=301,L]

    #...rewrite rules related to the www version of mydomain go here

    #The below rules should apply to any non WWW pages, such as subdomain.mydomain.com
    RewriteCond %{HTTP_HOST} ^(.*)\.mydomain\.com$

    RewriteRule ^products/?((^\/*)/(.*))$ product_info.php?products_id=$1&Path=$2&%{QUERY_STRING} [QSA]

    How does this look? Any obvious reason why it doesn't work? Or does syntax look correct?

    Thanks!
     
    Lucky Bastard, Oct 23, 2005 IP
  14. johnt

    johnt Peon

    Messages:
    178
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #14
    I can see a couple of problems here. The lines
    RewriteCond %{HTTP_HOST} !^www\.mydomain\.com [NC]
    RewriteRule ^(.*) http://www.mydomain.com/$1 [R=301,L]
    Code (markup):
    will change anything that is not www.mydomain.com into www.domain.com, and I don't think that you want to do that. Also, in your subdomain rewrite you are not using the subdomain itself.
    Try
    Options -Indexes
    RewriteEngine on
    RewriteBase /
    
    #...rewrite rules related to the www version of mydomain go here
    
    #The below rules should apply to any non WWW pages, such as subdomain.mydomain.com
    RewriteCond %{HTTP_HOST} !^www\.mydomain\.com$
    RewriteCond %{HTTP_HOST} ^(.*)\.mydomain\.com$
    
    RewriteRule ^products/?((^\/*)/(.*))$ product_info.php?subdomain=%1&products_id=$1&Path=$2&%{QUERY_STRING} [QSA]
    Code (markup):
    In the RewriteRule, replace "subdomain" with the correct variable name for your script.

    cheers

    John
     
    johnt, Oct 24, 2005 IP
  15. Lucky Bastard

    Lucky Bastard Peon

    Messages:
    406
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #15
    This was intended to rewrite mydomain.com to www.mydomain.com, and not rewrite subdomain.mydomain.com to www.mydomain.com.

    I need three types of rewrite rules:
    1) mydomain.com gets rewritten to www.mydomain.com
    2) such as: www.mydomain.com/something/else gets rewritten to www.mydomain.com/page.html?var1=$1&var2=$2
    3) subdomain rewrite rules: RewriteRule ^products/?((^\/*)/(.*))$ product_info.php?subdomain=%1&products_id=$1&Path=$2&%{QUERY_STRING} [QSA]

    Can you mod what I originally had so that happens?

    Thanks so much for your assistance John
     
    Lucky Bastard, Oct 24, 2005 IP
  16. johnt

    johnt Peon

    Messages:
    178
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #16
    Options -Indexes
    RewriteEngine on
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^mydomain\.com [NC]
    RewriteRule ^(.*) http://www.mydomain.com/$1 [R=301,L]
    
    #...rewrite rules related to the www version of mydomain go here
    RewriteCond %{HTTP_HOST} ^www\.mydomain\.com
    RewriteRule ^(^\/*)/(.*)$ page.html?var1=$1&var2=$2
    
    #The below rules should apply to any non WWW pages, such as subdomain.mydomain.com
    RewriteCond %{HTTP_HOST} ^(.*)\.mydomain\.com$
    RewriteRule ^products/?((^\/*)/(.*))$ product_info.php?subdomain=%1&products_id=$1&Path=$2&%{QUERY_STRING} [QSA] 
    Code (markup):
    should do it I think.
     
    johnt, Oct 24, 2005 IP
  17. Lucky Bastard

    Lucky Bastard Peon

    Messages:
    406
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #17
    Thanks SO much JohnT, all seems to be working, though I had to change the last line from:
    RewriteRule ^products/?((^\/*)/(.*))$ product_info.php?products_id=%1&Path=$1&%{QUERY_STRING} [QSA]
    to
    RewriteRule ^products/?((.*)\/(.*))$ product_info.php?products_id=%1&Path=$1&%{QUERY_STRING} [QSA]

    I must admit I don't know the difference, between :
    (^\/*)/(.*)
    and
    (.*)\/(.*)
     
    Lucky Bastard, Oct 24, 2005 IP
  18. johnt

    johnt Peon

    Messages:
    178
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #18
    (^\/*)/(.*) was supposed to match anything except a /, but maybe it doesn't need escaping with the \ here.
    Anyway, as long as it works all's well that ends well. :)
     
    johnt, Oct 24, 2005 IP
  19. mrugesh78

    mrugesh78 Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #19
    hello,

    I want to redirect : m. domain. com to m. domain.com/?page_id=2450

    I want to redirect only subdomain name. (not its internal files)

    i wrote following but not working properly

    Options +FollowSymlinks
    RewriteEngine on
    RewriteCond %{HTTP_HOST} .
    RewriteCond %{HTTP_HOST} ^m\.domain\.com$
    RewriteRule ^$ /?page_id=2450 [R=301,L]

    how I can do that. Please any one can help ?
     
    mrugesh78, Oct 13, 2008 IP