homepage Welcome to WebmasterWorld Guest from 54.166.14.218
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
mod rewrite for subdomains
mod_rewrite subdomain rewrite apache .htaccess
dareRock

5+ Year Member



 
Msg#: 3046391 posted 2:06 am on Aug 15, 2006 (gmt 0)

Hey there.....

I've been trying to get my mod_rewrite script working and am having no luck and thought i'd give WebmasterWorld a shot...since i collected a lot of info about here anyway....

I am trying to rewrite subdomains into queries for the top-level domain...

e.g. www.purple.colours.com rewrites to www.colours.com/show-colour.rhtml?colour=purple

I have made the neccessary change in the http.conf file for Apache to allow *.colours.com (wildcard sudomains)- well, i got my admin to do it.

my rewrite looks like this...

Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST}!^(www\.)?colours\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([\.]+)\.colours\.com$ [NC]
RewriteRule .* /show-colour.rhtml?colour=$1 [L]

now, any time i go to www.purple.colours.com, the index.rhtml page in the top-level directory (public_html for the top-level domain)always comes up instead of show-colour.rhtml.

I have a feeling i'm missing something thats simple, but i'm bangin my head here and could use some help if available.....thanks

 

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3046391 posted 3:30 am on Aug 15, 2006 (gmt 0)

That's because your substitution URL is back-referencing "$1" which is undefined. If you want to back-reference the subdomain ("purple," in your example) extracted by your RewriteCond, that variable is "%1", not "$1" ($1-$9 refer to the RewriteRule's parenthesized pattern matches, while %1-%9 refer to the (single) most-recently-matched RewriteCond's parenthesized pattern matches).

In addition, your code won't accept "www.purple.example.com" as written. You can fix that, and simplify your first RewriteCond as well:

Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([\.]+)\.example\.com
RewriteRule .* /show-colour.rhtml?colour=%2 [L]

Don't end-anchor your hostnames or your rules can be easily and intentionally broken by appending a port number to the hostname.

Jim

dareRock

5+ Year Member



 
Msg#: 3046391 posted 3:56 am on Aug 15, 2006 (gmt 0)

thanks for the tips jim....and the rework of the rewrite script....

i plugged it in and it still seems to be ignoring the last line, thus presenting the index.rhtml file......

is it possible that my admin made all the wildcard domains point to the '/index.rhtml' or '/', thus the last rule is ignored - i'm thinking no, b/c isn;t that the main function of mod-rewrite - overwrite the server defined rules?

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3046391 posted 12:25 pm on Aug 15, 2006 (gmt 0)

I'm not clear on what you mean by 'last rule' because there is only one rule with two conditions.

Some standard testing questions:

  • Do you have other RewriteRules in this file?
  • What URL did you request when testing? (please substitute 'example.com' for posting here)
  • What were the results when you requested that URL?
  • How did the results differ from your expectations?
  • Is there any indication of the problem in your Server Error Log?
  • Did you flush your browser cache before each test to prevent seeing stale results?

    The ruleset as shown should internally rewrite requests for www.purple.example.com or purple.example.com to /show-colour.rhtml?colour=purple as long as the code resides in a directory-path that is accessed for those requests. If your admin used a 'control panel' to set up your wild-cards, it's possible that they are pointed to a different directory or directories than the 'main' domain and www subdomain. If that is the case, then the code won't be executed and can have no effect on the request.

    Jim

  • dareRock

    5+ Year Member



     
    Msg#: 3046391 posted 12:45 pm on Aug 15, 2006 (gmt 0)

    sorry....'last rule' means the only rule in the code....i was calling the conditions rules aswell...sorry...

    Do you have other RewriteRules in this file? No

    What URL did you request when testing? (please substitute 'example.com' for posting here) www.purple.example.com

    What were the results when you requested that URL? no results, it just displayed the index.rhtml page rather than show-colour.rhtml

    How did the results differ from your expectations? i expected it to work and it didn't - again, index.rhtml showed and not show-colour.rhtml

    Is there any indication of the problem in your Server Error Log? error_log has no erros that relate to this, just old errors from before i implemented the rewriting.

    Did you flush your browser cache before each test to prevent seeing stale results? yes

    I'm waiting to hear back if my admin set the wildcard subdomains to point to some other directory or directories....ill keep you posted....thanks jim!...very much appreciated.

    jdMorgan

    WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



     
    Msg#: 3046391 posted 2:47 pm on Aug 15, 2006 (gmt 0)

    You may also need to add another RewriteCond at the top, in case your code is looping:

    RewriteCond %{REQUEST_URI} !^/show-colour\.rhtml

    Otherwise, the code will rewrite /show-colour.rhtml to itself repeatedly, until the browser or server hits its redirection limit (I would expect an error log entry in this case, though).

    Jim

    Philosopher

    WebmasterWorld Senior Member 10+ Year Member



     
    Msg#: 3046391 posted 2:54 pm on Aug 15, 2006 (gmt 0)

    Just a quick question.

    Are you sure you were actually using "www.purple.example.com" and NOT "purple.example.com"?

    Just wanting to make sure you aren't typing the www. out of habbit here.

    jdMorgan

    WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



     
    Msg#: 3046391 posted 3:02 pm on Aug 15, 2006 (gmt 0)

    It should have worked either way with the modified code posted above...

    The "www." is optional in the second RewriteCond, while the first RewriteCond prevents rewriting "www.example.com" to a subdirectory. Note that %1 may contain "www" if present, but the back-reference in the RewriteRule is to %2, which would contain the subdomain.

    As shown, the only requests it won't accept are for subdomains like "purple.www.example.com"

    Jim

    dareRock

    5+ Year Member



     
    Msg#: 3046391 posted 4:48 pm on Aug 15, 2006 (gmt 0)

    phil...i do both as I know the rewriteconds jim provided will take care of that variation...thanks

    jim, my admin placed (as suspected)....
    ServerAlias example.com *.example.com
    into the conf file for apache

    im wondering if you are right about the looping and will try that right now...thanks

    dareRock

    5+ Year Member



     
    Msg#: 3046391 posted 5:05 pm on Aug 15, 2006 (gmt 0)

    You may also need to add another RewriteCond at the top, in case your code is looping:

    RewriteCond %{REQUEST_URI}!^/show-colour\.rhtml

    Otherwise, the code will rewrite /show-colour.rhtml to itself repeatedly, until the browser or server hits its redirection limit (I would expect an error log entry in this case, though).

    I now have and tested this...
    [2]
    Options +FollowSymLinks
    RewriteEngine on
    RewriteBase /
    RewriteCond %{REQUEST_URI}!^/show-colour\.rhtml
    RewriteCond %{HTTP_HOST}!^www\.example\.com [NC]
    RewriteCond %{HTTP_HOST} ^(www\.)?([\.]+)\.example\.com
    RewriteRule .* /show-colour.rhtml?colour=%2 [L]
    [/2]

    ...same result as before, goes to my index.rhtml and passes no params....(note: i do have spaces b/w the '}' and '!')
    I also tried...

  • escaping the '.' in the rewriteRule[/li]
  • modifying my index.rhtml to show the var 'colour' and the rewriteRule to index.rhtml instead of show-colour.rhtml[/li]
    ...with no luck - i find it weird that the second point (making the change to rewriteRule to index.rhtml) didn't show the value of colour - which i added to the code.

  • jdMorgan

    WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



     
    Msg#: 3046391 posted 5:14 pm on Aug 15, 2006 (gmt 0)

    Well, something's interfering with this rule, or perhaps mod_rewrite is not being invoked.

    Try temporarily changing the syntax so that an external redirect is invoked. This will allow you to see the code working if the rule is applied:

    Options +FollowSymLinks
    RewriteEngine on
    RewriteBase /
    RewriteCond %{REQUEST_URI} !^/show-colour\.rhtml
    RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
    RewriteCond %{HTTP_HOST} ^(www\.)?([\.]+)\.example\.com
    RewriteRule .* http://www.example.com/show-colour.rhtml?colour=%2 [R=302,L]

    Otherwise, I'd suggest testing with a simple unconditional rule first, something like:

    RewriteRule ^file_that_does_not_exist$ http://www.example.com/file_that_does_exist [R=302,L]

    Request the file that does not exist from your main domain and also from your subdomains. The result should be that the server serves up the file that does exist. Flush your browser cache before each test.

    This will indicate whether mod_rewrite is even being invoked.

    There is no need to escape characters in the substitution URL. This is only necessary within regular-expressions patterns.

    Jim

    dareRock

    5+ Year Member



     
    Msg#: 3046391 posted 5:17 pm on Aug 15, 2006 (gmt 0)

    btw...no error_log for any of the above attempts

    Philosopher

    WebmasterWorld Senior Member 10+ Year Member



     
    Msg#: 3046391 posted 5:31 pm on Aug 15, 2006 (gmt 0)

    Sorry...didn't look closely enough at the updated code to see that was being looked for.

    What was the outcome of the last two examples jdMorgan had you try?

    Did the external redirect actually occur? If so, what was in the query string?

    What about the lone redirect rule?

    Or did you still get nothing at all, but also no error logs?

    [edited by: Philosopher at 5:31 pm (utc) on Aug. 15, 2006]

    dareRock

    5+ Year Member



     
    Msg#: 3046391 posted 5:38 pm on Aug 15, 2006 (gmt 0)

    jim...i used your latest example above and nothing changed...

    i ran a test using...
    [2]RewriteRule ^colour/(.*)$ http://www.example.com/$1.rhtml [R=302,L][/2]

    i typed into the FF browser...
    [2]http://www.example.com/colour/purple[/2]

    and got a 404 for /purple.rhtml

    i cleared my cache each time i tested.

    so, from this, we know mod_rewrite is working - it must be something in that last rule we are missing....

    dareRock

    5+ Year Member



     
    Msg#: 3046391 posted 5:39 pm on Aug 15, 2006 (gmt 0)

    *'last rule' - i meant just 'rule'

    dareRock

    5+ Year Member



     
    Msg#: 3046391 posted 5:56 pm on Aug 15, 2006 (gmt 0)

    sorry phil i missed your comment...

    when i tested Jim's code, the redirect didn't occur - i assume i would have seen the url change from..
    www.purple.example.com to www.example.com/show-colour.rhtml?search=purple

    ...which it did not, it stayed the same (www.purple.example.com)

    for the second, single line test....it appeared to work...it generated a 404 since i was testing for a file that wasn't there...

    i used...
    RewriteRule ^colour/(.*)$ http://www.example.com/$1.rhtml [R=302,L]
    ...as my rule

    i typed into the FF browser...
    http://www.example.com/colour/purple
    ...and got a 404 for /purple.rhtml

    i cleared my cache each time i tested.

    so, from this, we know mod_rewrite is working

    Philosopher

    WebmasterWorld Senior Member 10+ Year Member



     
    Msg#: 3046391 posted 7:28 pm on Aug 15, 2006 (gmt 0)

    It looks like the RewriteCond statement is not being met. If it was jdMorgan's last attempt with a very greedy rule should have worked.

    jdMorgan is definitely far better at this stuff than me but it sure seems like the information being returned in the HTTP_HOST variable is not what is being expected and tested for.

    I'd check into exactly what is being returned in the HTTP_HOST variable.

    dareRock

    5+ Year Member



     
    Msg#: 3046391 posted 8:06 pm on Aug 15, 2006 (gmt 0)

    how do i determine what exactly is in the HTTP_HOST?

    Philosopher

    WebmasterWorld Senior Member 10+ Year Member



     
    Msg#: 3046391 posted 8:25 pm on Aug 15, 2006 (gmt 0)

    An easy way would be to use the following code. Cut and paste it into file and save it as phpinfo.php or something like that. FTP it to your server then access it via your browser.

    Under the "apache settings" section, check what is shown for HTTP_HOST


    <?php
    phpinfo();
    ?>

    Just be sure to later remove it as it shows all the environmental variables for your server.

    If this isn't a live site. You could replace your index file with this code and see what the HTTP_HOST variable is returning for full domain request, subdomain requests etc. as well since everything seems to pull up your index file.

    dareRock

    5+ Year Member



     
    Msg#: 3046391 posted 8:59 pm on Aug 15, 2006 (gmt 0)

    HTTP_HOST is www.example.com

    thats what it should be, right?

    Philosopher

    WebmasterWorld Senior Member 10+ Year Member



     
    Msg#: 3046391 posted 9:09 pm on Aug 15, 2006 (gmt 0)

    Yep...the next question is what is the HTTP_HOST variable showing when you try and access it via a subdomain such as what you've been using (www.purple.example.com).

    I'm wondering...can you access the file with the code I gave you by something like

    www.purple.example.com/phpinfo.php

    ?

    If so, you will want to see what it shows as the HTTP_HOST variable when accessing it via a subdomain.

    dareRock

    5+ Year Member



     
    Msg#: 3046391 posted 9:14 pm on Aug 15, 2006 (gmt 0)

    yes, i can...when i do...
    www.purple.example.com/phpinfo.php
    ...the HTTP_HOST is www.purple.example.com

    jdMorgan

    WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



     
    Msg#: 3046391 posted 9:21 pm on Aug 15, 2006 (gmt 0)

    Mea culpa... Looks like it was a typo:

    Options +FollowSymLinks
    RewriteEngine on
    RewriteBase /
    RewriteCond %{REQUEST_URI} !^/show-colour\.rhtml
    RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
    RewriteCond %{HTTP_HOST} ^(www\.)?[b]([^\.]+)[/b]\.example\.com
    RewriteRule .* http://www.example.com/show-colour.rhtml?colour=%2 [R=302,L]

    The "^", meaning NOT, was missing.

    Jim

    [edited by: jdMorgan at 9:22 pm (utc) on Aug. 15, 2006]

    Philosopher

    WebmasterWorld Senior Member 10+ Year Member



     
    Msg#: 3046391 posted 9:23 pm on Aug 15, 2006 (gmt 0)

    hmm...that blows that theory out of the water.

    So the proper HTTP_HOST variable is being returned yet the rewrite is still not happening even though we know that mod_rewrite is active on your server.

    I'm stumped. The code should be working then unless something your host has setup in the config files is interfering with it.

    Maybe jdMorgan or someone else will have another idea. If not, I'd check with the support department of your host and give them the code and see if they know of something in their setup that would be interfering with it.

    dareRock

    5+ Year Member



     
    Msg#: 3046391 posted 9:49 pm on Aug 15, 2006 (gmt 0)

    as the crowd chants 'JD', 'JD', 'JD', he bows his head modestly as though he doesn't deserve the praise he is receiving....

    it worked!....now, i just have to figure out how to get my style sheets and javascript to work.....

    i imagine thats a few more rules and/or conditions...is that right?

    dareRock

    5+ Year Member



     
    Msg#: 3046391 posted 10:57 pm on Aug 15, 2006 (gmt 0)

    so i added...

    RewriteCond %{REQUEST_URI}!^/images/
    RewriteCond %{REQUEST_URI}!^/_javascripts/
    RewriteCond %{REQUEST_URI}!^/css/

    ...and everything works...just getting javascript errors, but im gonna keep plugging at it to get them clear.....

    Thanks for your help JD and Phil! very much appreciated!

    Global Options:
     top home search open messages active posts  
     

    Home / Forums Index / Code, Content, and Presentation / Apache Web Server
    rss feed

    All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
    Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
    WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
    © Webmaster World 1996-2014 all rights reserved