Forum Moderators: phranque

Message Too Old, No Replies

rewrite to a subdomain?

         

nfs2

10:03 pm on Jan 2, 2006 (gmt 0)

10+ Year Member



Is it possible to make www.mysite.com/profile.php?user=somebody into somebody.mysite.com?

The "somebody" would have to be a wildcard since its a multi user site.. I just need to knnow if mod rewrite can do this or if i should look for another solution

jdMorgan

12:22 am on Jan 3, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, yes and no...

You will need to 'publish' your links on your pages using the subdomain naming convention, and then use mod_rewrite to rewrite those links, when requested from your server, back into the form needed to call your script. mod_rewrite works on URLs as they are received by your server during the URL-to-filename translation phase, and has no capability to change the content of your pages (in the content-handler phase). As a result, mod_rewrite works 'backwards' from the perspective of your question, but it *is* used to implement subdomain-handling with a scripted application.

Try a site search for subdomain rewriterule rewritecond HTTP_HOST script and variations -- there are several previous threads on this subject, and some background information in the forum charter [webmasterworld.com] and tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Jim

nfs2

2:21 pm on Jan 3, 2006 (gmt 0)

10+ Year Member



Thanks for your reply. I searched the site and found what i thought would be the solution here [webmasterworld.com...]

It seems he wants to do exactly what i want. I just replaced mydomain with my actual site name, and replaced index.php with profile.php. I thought it would work, but when i go to myname.mydomain.com, i get a "server cannot be found" error, and when i go to www.mydomain.com, i get a "username not registered" error..

:(

jdMorgan

7:59 pm on Jan 3, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> i get a "server cannot be found" error

You will need to do three things to make this code work:

  • Add a 'wildcard' A record to your DNS zone file, to point requests for any subdomain to the IP address of your server. By default, only example.com and www.example.com will usually work.
  • Configure your server to 'point' requests for any subdomain to the same directory as your main domain, or ask your host to set up this 'wildcard' servername.
  • Install code in your top-level Web directory to 'point' all subdomain requests (except for 'www') to the correct folder.

    If you have admin access to httpd.conf, you can skip the second two steps, and set up virtual hosts for your subdomains.

    > and when i go to www.mydomain.com, i get a "username not registered" error.

    This doesn't sound like an Apache error at all. It may be one of your scripts, or one of your hosting companies scripts -- I can't tell.

    Jim

  • nfs2

    8:14 pm on Jan 3, 2006 (gmt 0)

    10+ Year Member



    I dont think i have access to all that stuff, i use a hosting company and dont have my own server..

    The "username cannot be found" error is from my own code. It means sombody tried to view the profile of a user that doesnt exist. As if www was a username.

    jdMorgan

    8:26 pm on Jan 3, 2006 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    Well, 'www' *is* a subdomain, so if you don't want it treated as a username, you'll have to explicitly exclude it from being rewritten. Just add a RewriteCond to the version of the code that gave you this error -- that version sounds like it's 'almost working'.

    Jim

    nfs2

    8:42 pm on Jan 3, 2006 (gmt 0)

    10+ Year Member



    I think your right, i think this is very close to working, maybe a while more of toying with it will get it working right... I'll let you know how it works out

    nfs2

    9:04 pm on Jan 3, 2006 (gmt 0)

    10+ Year Member



    Heres the code im working with. Its the exact code that you put in the other thread, except i changed "mydomain" to "mysite"(my actual domain, u know..) and .com to .net, and "show" and "index" both to "profile".

    That should work, but on the www.mysite.net, i get the "username not found" error, and when i enter an existing username like username.mysite.net i get a "cannot find server".. Very strange. The code im using is below

    RewriteEngine on

    # If no-www domain requested, externally redirect to www domain
    RewriteCond %{HTTP_HOST} ^mysite\.net
    RewriteRule (.*) [mysite.net...] [R=301,L]
    #
    # If www+subdomain domain requested, externally redirect to subdomain without "www"
    RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.mysite\.net
    RewriteRule (.*) [%1.mysite.net...] [R=301,L]
    #
    # If subdomain+www domain requested, externally redirect to subdomain without "www"
    RewriteCond %{HTTP_HOST} ^([^.]+)\.www\.mysite\.net
    RewriteRule (.*) [%1.mysite.net...] [R=301,L]
    #
    # If www domain requested, rewrite html page requests to profile.php with query string
    RewriteCond %{REQUEST_URI}!^/profile\.php
    RewriteCond %{HTTP_HOST} ^www\.mysite.net
    RewriteRule ^([^.]+)\.html /profile.php?page=$1 [L]
    #
    # If subdomain requested, rewrite subdomain and html page requests to profile.php with query string
    RewriteCond %{REQUEST_URI}!^/profile\.php
    RewriteCond %{HTTP_HOST}!^www\.mysite\.net
    RewriteCond %{HTTP_HOST} ^([^.]+)\.mysite\.net
    RewriteRule ^([^.]+)\.html /profile.php?user=%1&page=$1 [L]
    #
    # If www domain requested, rewrite home page requests to profile.php with query string page = "home"
    RewriteCond %{REQUEST_URI}!^/profile\.php
    RewriteCond %{HTTP_HOST} ^www\.mysite.net
    RewriteRule ^$ /profile.php?page=home [L]
    #
    # If subdomain requested, rewrite home page requests to profile.php with query string user=subdomain & page="home"
    RewriteCond %{REQUEST_URI}!^/profile\.php
    RewriteCond %{HTTP_HOST}!^www\.mysite\.net
    RewriteCond %{HTTP_HOST} ^([^.]+)\.mysite\.net
    RewriteRule ^$ /profile.php?user=%1&page=home [L]

    jdMorgan

    11:05 pm on Jan 3, 2006 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    I don't see anything seriously wrong with that. There were several unneeded RewriteConds (you excluded rewrites of profile.php using a RewriteCond, but the following RewriteRule only applied to .html files, so the RewriteCond was not needed). I also re-arranged a bit of the logic to eliminate three rulesets completely, and end-anchored a couple of patterns.

    # If non-www domain requested, externally redirect to www domain
    RewriteCond %{HTTP_HOST} ^mysite\.net
    RewriteRule (.*) http://www.mysite.net/$1 [R=301,L]
    #
    # If www+subdomain or subdomain+www domain requested, externally redirect to subdomain without "www"
    RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.mysite\.net [OR]
    RewriteCond %{HTTP_HOST} ^([^.]+)\.www\.mysite\.net
    RewriteRule (.*) http://%1.mysite.net/$1 [R=301,L]
    #
    # if home page requested, rewrite to 'home.html' and pass through to following rules
    RewriteRule ^$ /home.html
    #
    # If www domain requested, rewrite html page requests to profile.php with query string
    RewriteCond %{HTTP_HOST} ^www\.mysite.net
    RewriteRule ^([^.]+)\.html$ /profile.php?page=$1 [L]
    #
    # If subdomain requested, rewrite subdomain and html page requests to profile.php with query string
    RewriteCond %{HTTP_HOST} !^www\.mysite\.net
    RewriteCond %{HTTP_HOST} ^([^.]+)\.mysite\.net
    RewriteRule ^([^.]+)\.html$ /profile.php?user=%1&page=$1 [L]

    What I'd suggest here is that you comment out all but one ruleset at a time (except for the third one when testing either the fourth or fifth, since they both depend on it). Then test the function of just the active rule, and see which one(s) are causing trouble. We need to narrow the focus down to a smaller problem in order to make any progress.

    Once you have determined where the problem is, then look at your raw server error log after requesting a URL that will activate rule and cause the/an error, and see what the server's specific complaint is. This could be caused by any number of things -- including a few that I didn't list in my first post. but your code is basically OK.

    Jim

    nfs2

    12:29 am on Jan 4, 2006 (gmt 0)

    10+ Year Member



    Just out of curiosity, does the above code support wildcards? Like, if i put blablabla.mysite.com it would try to display mysite.com/profile.php?user=blablabla or whatever i put right? Cause i thought i needed (.*) to make a wildcard..

    nfs2

    1:29 am on Jan 4, 2006 (gmt 0)

    10+ Year Member



    Well i figured out what was causing that "username not found" error. It wasnt thinking "www" was a uswername, it was part of the code that went profile.php?user=home. So it thought home was a username.

    Basically anything besides www as a sub domain causes a server not found error. Is this something i have to take up with my host?

    jdMorgan

    1:33 am on Jan 4, 2006 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    This you may be able to do yourself:

  • Add a 'wildcard' A record to your DNS zone file, to point requests for any subdomain to the IP address of your server. By default, only example.com and www.example.com will usually work.

    For this one, contact your host:

  • Configure your server to 'point' requests for any subdomain to the same directory as your main domain, or ask your host to set up this 'wildcard' servername.

    Jim

  • jdMorgan

    1:46 am on Jan 4, 2006 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    > Cause i thought i needed (.*) to make a wildcard..

    No, not necessarily. ".*" is the wildest of wildcards, and means "any number (including zero) of any characters."

    In the example code here, we use "[^.]+" instead, meaning "one or more characters, not equal to a period (dot, full stop, etc.). It is a far more efficient pattern, because it allows the regex parser to immediately know where to stop matching characters into the %1 variable: We told it to stop when it finds the period.

    Otherwise, using ".*", the parser will throw the entire requested hostname into %1, and then realize it can't match the rest of the pattern. So, it will back off one character from the end and try again, repeating this backoff-and-try-again until it finally gets a match with only the subdomain left in %1. Therefore, the required number of parser passes is directly proportional to the length of the string folling the subdomain name in the requested hostname. Using the "[^.]+" pattern, the parser only needs one pass.

    Avoid using ".*" whenever possible. It is possibly the easiest pattern to learn/use, but also the least efficent and often the most troublesome. The functional descriptors for the ".*" pattern are "ambiguous," "greedy," and "promiscuous." Although these terms are used to describe its technical pattern-matching behaviour, they still carry the stigma normally associated with those words.

    Jim

    nfs2

    1:46 am on Jan 4, 2006 (gmt 0)

    10+ Year Member



    I dont think i have access to anything called a zone file, but in cPanel i have a subdomain management tool, that lets me put [blank].mysite.net. Can i put a wildcard there? Or is that something different..

    nfs2

    2:10 am on Jan 4, 2006 (gmt 0)

    10+ Year Member



    Ok i just talked to my host and found out that they dont support wildcard domains.. Oh well, thanks a lot for your help

    Instead of username.mysite.com, im gonna try making it www.mysite.com/username. Hopeully it'll work

    nfs2

    3:51 pm on Jan 4, 2006 (gmt 0)

    10+ Year Member



    Is there a way to modify this code to make it www.mysite.com/username instead of a subdomain? Everything i tried so far results in a server error..