homepage Welcome to WebmasterWorld Guest from
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

Subdomain - variable Mod Rewrite doesnt work. Please help

 5:44 pm on Oct 16, 2005 (gmt 0)

Here's the script I created:

RewriteEngine on
Options +FollowSymlinks
RewriteCond %{REQUEST_URI}!^/index\.php
RewriteCond %{HTTP_HOST}!^www\.
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com
RewriteRule .* [domain.com...] [nc]

What i want to achieve is that urls of

[variable.domain.com...] will access the page [domain.com...]

However, the address bar should still show [variable.domain.com....]

This script doesnt work... I don't know why. This is the error i get from the page:

This is the Plesk™ default page
If you see this page it means:

1) hosting for this domain is not configured
2) there's no such domain registered in Plesk.

For more information please contact Administrator.

Please help, I've been researching the net and tried so many but still I failed.



 5:58 pm on Oct 16, 2005 (gmt 0)


Welcome to WebmasterWorld!

If you wish to reference the value of the subdomain stored by the RewriteCond back-reference, you must use %1, not $1 in the RewriteRule substitution. See the [url=http://httpd.apache.org/docs/mod/mod_rewrite.html]mod_rewrite documentation{/url}.



 6:12 pm on Oct 16, 2005 (gmt 0)

Thank you very much for welcoming me. :)

I replaced $1 to %1, but still doesnt work.

I tried simple file rewriting rules and it worked, but when i tried creating script involving subdomains and subdirectories, it doesnt work.

I used Plesk VPS on my server, any ideas?

Please help.


 8:39 pm on Oct 16, 2005 (gmt 0)

I don't use Plesk, but the following code is correct:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/viewforum\.php$
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule .* http://www.example.com/viewforum.php?f=%1 [L]

Also, "It doesn't work" is not enough information to determine what the problem might be.



 1:39 am on Oct 17, 2005 (gmt 0)

simple rewriting rules worked on me like redirecting or renaming the URL in the same directory but when i tried to rewrite URL that has subdomains and folder it doesn't work. I don't know why.

i also created this rewrite rule to redirect from [domain.com...] to [domain.com...] .

RewriteRule ^http://www.domain.com/artist/(.*)$ [domain.com...] [nc]

This one doesn't work too.. any ideas? please help


 2:26 am on Oct 17, 2005 (gmt 0)

Please state explicitly what you mean by "It doesn't work." For example:

What URL did you request?
What do you see in your browser screen?
What do you see in the browser address bar?
What do you see in your server access log?
What do you see in your server error log?

And finally, how do the results differ from what you expect? In some cases, it is the expectations that are incorrect, and not the implementation.



 2:58 am on Oct 17, 2005 (gmt 0)

--What URL did you request?

I typed in the address bar:

and this page should access:

--What do you see in your browser screen?
instead i see this error page.

This is the Plesk™ default page
If you see this page it means:

1) hosting for this domain is not configured
2) there's no such domain registered in Plesk.

For more information please contact Administrator.

---What do you see in the browser address bar?

I simply want a way for users to easily manage a redirect on their subdomains?


points to www.domain.com/viewforum.php?f=band

I know that, cpanel, does this quite well and it is a feature that I require. But im using Plesk

I want to allow the URLs viewforum.php query string variable at the domain's root to be accessed as though they are a subdomain

I'm very sory for i'm a beginer in this mod_rewrite, i also dont know where to access the server and error logs.


I hope this is clear, forgive my english, im chinese.


 6:27 pm on Oct 17, 2005 (gmt 0)

I think the error message is telling you the problem:
1) hosting for this domain is not configured
2) there's no such domain registered in Plesk.

In order for your code to work, the serve must know to associate your subdomains with a particular DocumentRoot. Any configuration code you then place in httpd.conf in that DocumentRoot, or in .htaccess files below that DocumentRoot, will then run whenever a request is made to a URL-path in that subdomain.

In order for this to work, three steps are needed:

  • DNS for each subdomain (or wild-card DNS for *all* subdomains) must be configured so that the subdomain resolves to your server's IP address.

  • Your server must be configured to "deliver" requests for these subdomains to the file-area defined for your hosting account.

  • Code in your account's file area must detect the requested subdomain and pass requests to the proper subdirectory.

    If step 1 was missing, you would get domain resolution errors, "This host cannot be found" messages. Your code is what you need for step 3. But it sound to me like step 2 is missing; the server does not know what to do with subdomain requests, so your code is never executed.

    In order to fix this, you'll probably need to
    ...please contact Administrator.


  • hp_solomon

     9:25 am on Nov 3, 2005 (gmt 0)


    I was able to overide that redirects the subdomain to its default directory by editing the top level vhost.conf.

    Now i got a new problem. When I logged in to the site and go to the page using a link or typing the URL of the fake subdomain (http://bandname.domain.com), my session dissapeared. Everytime I go to the page even I am logged in, I was logged out or not totally logged out because when i go back to the page without the subdomain, i was logged in again.

    Seems like using the subdomain my session dissapeared but the session is there when i go back to the page without the subdomain.

    Is there a way to maintain a single session to multiple subdomain, or is there a solution to fix this issue. Any advice.?

    Here is the directory structure of the site when i access it through FTP:
    conf --> root conf where vhost.conf is located
    httpdocs ---> root folder of the site... and the root of phpbb
    subdomains ---> empty, no folders and files - default directory of subdomains

    I think the solution to this is to point all my subdomain to the directory root of my domain name. that is on /httpdocs/ directory.

    I tried this script in my root vhost.conf but it doesnt work

    <IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteMap lowercase int:tolower
    ServerAlias *.domain.com
    RewriteCond %{REQUEST_URI}!^/index.php
    RewriteCond %{HTTP_HOST}!^www.domain.com$
    RewriteCond %{HTTP_HOST}!^webmail\.domain.com$
    RewriteCond %{HTTP_HOST} ^[^.]+\.domain.com$
    RewriteRule ^(.+) %{HTTP_HOST}$1 [C]
    RewriteRule .* [domain.com...] [P]
    RewriteRule ^([^.]+).domain.com(.*) /home/httpd/vhosts/domain.com/httpdocs/$2 [L]

    The session is lost because of the mod rewrite. I guess the reason why is that the subdomain i generated through mod_rewrite still references the subdomain directory not the main domain directory where my session cookies are located.
    I'm thinking if there's a way to mod_rewrite the subdomain and will point the sub directories to the root subdomain then it might work.

    Again this is what I want to achieve. Changing [bandname.domain.com...] to access the page [domain.com...] without actually changing the URL in the address bar and loosing sessions.

    Please help for i've been trying to solve this for more than two weeks already. I'm loosing hope. I'm sure there is a way.


     3:30 pm on Nov 3, 2005 (gmt 0)

    The problem may be that you are creating an external redirect, when I suspect you want an internal rewrite.

    So, this might work better:

    ServerAlias *.domain.com
    RewriteEngine on
    # RewriteMap lowercase int:tolower
    RewriteCond %{REQUEST_URI} !^/viewforum\.php$
    RewriteCond %{HTTP_HOST} !^(www¦webmail)\.domain\.com
    RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com
    RewriteRule . /viewforum.php?f=%1 [L]
    # RewriteRule ^([^.]+).domain.com(.*) /home/httpd/vhosts/domain.com/httpdocs/$2 [L]

    I don't know why you were using a proxy throughput, the RewriteMap was defined, but never invoked (now commented-out), and I can't fathom the purpose of your last rule (also now cemmented-out) at all.

    I assume that the first RewriteCond was just copied and pasted from an existing code example. The usual intent of this type of construct is to prevent an 'infinite' rewrite loop if the request has already been rewritten to the "page" in the final rewriterule. So I have changed the first RewriteCond from testing for "/index.php" to "/viewforum.php" to prevent such a loop.

    In addition, the user-requested "page name" will be discarded (subject to your exclusions), and all requests except for "/viewforum.php" or the pages in the "www" or "webmail" subdomains will be rewritten to "/viewforum.php". I hope that's what you want, but I don't know.

    It is possible that your sessions cookie is domain-based. Because you were doing a redirect or proxy throughput, that domain was changing, so the session no longer applied. If the above does not "work better", then you'll need to dig into the logic underlying session tracking to find out what dependencies apply to sessions.

    Replace all broken pipe "¦" characters above with solid pipes before use; Posting on this forum modifies them. Flush your browser cache after any change to the code.



     11:16 pm on Nov 3, 2005 (gmt 0)

    I tried the script and it worked. I set the cookie domain to .domain.com and path is "/" and I was able to change from the main domain to the variable subdomains without loosing sessions.

    Im so happy that after so many retries I finally found the answers from you. Thank you very much.

    Now I still have a problem (we're almost there)

    all images, css, all links are now gone.. they are now pointed relative to the subdomain, not to the document root of the domain name... example, images and links are now pointed to [bands.subdomain.com...] not to [domain.com...] I'm thinking to add this script at the end but not sure if this will work.

    RewriteRule ^[^.].domain.com /home/httpd/vhosts/domain.com/httpdocs/ [L]

    Any solution to this? a rewrite rule maybe? or should i update all links to absolutely map to the document root of the domain name?

    please advice. Thank you very much.


     1:21 pm on Nov 4, 2005 (gmt 0)

    Domains and subdomains don't exist inside your server, only directories and files, so it does not matter whether the images, css, etc. point to the subdomain or the main domain.

    What does matter is that there's nothing in the code above that prevents requests for these files from being rewritten to your php script.

    So, you may be able to cure this problem simply by excluding these requests from being rewritten by adding something like:

    RewriteCond %{REQUEST_URI} !\.(css¦gif¦jpe?g¦png)$

    to the beginning of the code.

    One thing that will help clarify this kind of problem for you is that it is the browser that resolves relative links on your pages to a canonical URL. So, if the browser sees a relative link on a page in one of your 'band' subdomains, it assumes that that link should be canonicalized to that subdomain. Since all of these subdomains actually resolve to the filespace of your main domain, that doesn't really matter, but unless your php script can handle the css and image requests, you won't want to rewrite these requests to the script.

    If you want to use canonical links to avoid this problem, you can. On the one hand, the URLs "look better" if they maintain the current subdomain path -- it looks better to the user. On the other hand, if you have any very large images and/or scripts that are used in all subdomains, and if your users tend to browse around in many subdomains, then there would be a small caching-related advantage to using canonical links on those resources, so that the client browser would know that it didn't need to re-fetch them when navigating to a page in a new subdomain.

    If you pursue a rewrite solution, be aware that RewriteRule cannot "see" the domain; You must use A RewriteCond testing %{HTTP_HOST} to test it or create a back-reference to it.



     4:14 am on Nov 5, 2005 (gmt 0)


    Considering performance, is it better to absolutely path the images or add a rewriteCond instead?

    The problem is that all the images doesnt display no more. When I type path/css images on the Adress Bar of the Browser it displays the band main page instead (http://band.domain.com/viewforum.php?f=band).

    All images/css are redirected to the main band page when i typed it in the address bar.

    What do you suggest, absolutely path the images or add a rewrite condition?

    Thank you very much.

    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