Welcome to WebmasterWorld Guest from 54.80.188.87

Forum Moderators: Ocean10000 & phranque

Message Too Old, No Replies

Subdomains with .htaccess

How to internally rewrite subdomains to subdirectories

     
12:55 am on May 13, 2005 (gmt 0)

New User

10+ Year Member

joined:Feb 22, 2005
posts:38
votes: 0


After searching for ages on the internet to solve my problem and reading a few dozen of topics and tried several scripts i cant get the subdomains to work on mijdomain.

Mijn host has set the ServerAlias * to my domain. The situation is as follows: i want to make a few hundreds of subdomains, all subdomains must internally point to the /home/httpdocs/subdomains/ map. But not the www.domain.com and domain.com. If there are domains that does not exist they must resolve to a 404 error.

I have tried the following from another topic:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^$ [NC]
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$ [NC]
RewriteCond %{HTTP_HOST}<->%{REQUEST_URI} ^(www\.)?([^.]+).*<->/([^/]+) [NC]
RewriteCond %2<->%3 !^(.*)<->\1$ [NC]
RewriteRule ^(.+) /%2/$1 [L]

But it seems to loop, and i'm not getting any subdomain to work. If i do not get a 505 or a loop the page redirects to [domain.com...] and does nothing. Can anyone help me?

[edited by: jdMorgan at 12:00 am (utc) on May 24, 2005]
[edit reason] Examplified. [/edit]

1:58 am on May 13, 2005 (gmt 0)

Senior Member

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

joined:Mar 31, 2002
posts:25430
votes: 0


The code you found will only work with operating systems that support POSIX 1003.2 regular expressions. FreeBSD is one, and there are a few others.

Another way to do this is to rewrite <subdomain>.example.com to /sd_<subdomain>/ -- That is, write each subdomain to a uniquely-named subdirectory. You could also name the subdirectories "user-<subdirectory>", or the familiar "~<subdomain>", or anything like that, but the prefix needs to be something that you 'reserve' for use only in naming first-level subdomain subdirectories.

In those cases, you could use:


# Rewrite subdomain requests to subdirectories except for www.example.com
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/sd_
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule (.*) /sd_%1/$1 [L]
#
# Redirect direct user-agent requests for www.example.com/sd_<subdomain>/<page> to http://<subdomain>.example.com/<page>
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /sd_(.+)\ HTTP/
RewriteRule ^sd_([^/]+)/(.*)$ http://$1.example.com/$2 [R=301,L]

The first section supports all subdomain requests, while the second section keeps users and search engines from finding the 'real' location of the files under www.example.com/sd_<subdomain>, thus preventing curiosity-seekers from finding them and duplicate-content problemns in search engine results.

Jim

10:22 am on May 13, 2005 (gmt 0)

New User

10+ Year Member

joined:Feb 22, 2005
posts:38
votes: 0


The first one works, but only after i had find out the [subdomain.example.com...] gets redirected to [example.com....] In other words people MUST type the slash after the subdomain. That's not the idea, subdomains with and without the slash need to to the same thins.

I very much appriciate your time, finally i've got some basic to start with.

[edited by: jdMorgan at 11:58 pm (utc) on May 23, 2005]
[edit reason] Examplified. [/edit]

2:18 pm on May 13, 2005 (gmt 0)

Senior Member

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

joined:Mar 31, 2002
posts:25430
votes: 0


There is nothing in the code above that is dependent on the trailing slash, or that affects the trailing slash.
If you have no other code in your .htaccess, then it is likely that your hosting configuration (httpd.conf) is causing this problem. Contact your hosting administrator and ask them why this is happening.

Two possibilities are:

  • UseCanonicalName [httpd.apache.org] is "on" or "DNS" -- It should be "off"
  • Mod_rewrite or mod_access code in httpd.conf that is doing this redirect

    There could also be some other problems -- these are just the most likely ones.

    Jim

  • 2:25 pm on May 13, 2005 (gmt 0)

    Senior Member

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

    joined:Mar 31, 2002
    posts:25430
    votes: 0


    Also, I typed the second RewriteCond incorrectly -- There should be no "$" at the end. The above information still applies.

    # Rewrite subdomain requests to subdirectories except for www.example.com
    RewriteEngine On
    RewriteCond %{REQUEST_URI} !^/sd_
    RewriteCond %{HTTP_HOST} !^www\.example\.[b]com[/b] [NC]
    RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
    RewriteRule (.*) /sd_%1/$1 [L]
    #
    # Redirect direct user-agent requests for www.example.com/sd_<subdomain>/<page> to http://<subdomain>.example.com/<page>
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /sd_(.+)\ HTTP/
    RewriteRule ^sd_([^/]+)/(.*)$ http://$1.example.com/$2 [R=301,L]
    9:58 am on May 17, 2005 (gmt 0)

    New User

    10+ Year Member

    joined:Feb 22, 2005
    posts:38
    votes: 0


    My host has mailed that the following things exist in httpd.conf:

    + LoadModule rewrite_module modules/mod_rewrite.so
    + UseCanonicalName Off

    Is this ok? Do the second option need to be "on"?

    10:50 am on May 17, 2005 (gmt 0)

    New User

    10+ Year Member

    joined:Feb 24, 2005
    posts: 20
    votes: 0


    this is how I have done it:

    I have moved my scripts to a webhotell, and had to use the .htaccess solution. Got a lot of 500 in the beginning, but now I seem to understand what is going on.

    <Files .*> #this is only for allowing non-type files, like domain.com/blog
    Order Deny,Allow
    Allow From All
    </Files>

    RewriteEngine On
    Options +FollowSymlinks
    RewriteBase /

    RewriteCond %{HTTP_HOST} ([^\.]*).domain.com$
    RewriteCond %{REQUEST_URI}!^/static/*
    RewriteCond %{REQUEST_URI}!^/[^/]+/index.php$
    RewriteCond %{REQUEST_URI}!^/favicon.ico$
    RewriteRule (.*) $/%1/index.php?$1&domainname=%{HTTP_HOST}%{REQUEST_URI} [L,QSA]

    Here is a subdomain calling a php script in a subfolder with the same name.
    Calls to /static/* are being ignored (pictures),
    or else everything else is being sent to the script.
    Remember that it is important to include this:
    RewriteCond %{REQUEST_URI}!^/[^/]+/index.php$
    for the script to be run, or else you get 500 error.

    Regards,
    Lars

    10:56 am on May 17, 2005 (gmt 0)

    New User

    10+ Year Member

    joined:Feb 24, 2005
    posts:20
    votes: 0


    you might want to do an additional check that the directory actually exist:

    RewriteCond %{HTTP_HOST} ([^\.]*).domain.com$
    RewriteCond /home/www/%1 -d

    Cheers,
    Lars

    11:40 am on May 17, 2005 (gmt 0)

    New User

    10+ Year Member

    joined:Feb 24, 2005
    posts:20
    votes: 0


    I was coming here to ask some questions, but I solved them myself - here is the result:

    RewriteCond %{HTTP_HOST} ([^\.]*).domain.com$
    RewriteCond /home/www/%1 -d
    RewriteCond %{REQUEST_FILENAME}!-f
    RewriteRule (.*) $/%1/index.php?$1&url=%{HTTP_HOST}%{REQUEST_URI} [L,QSA]

    So now my 500 errors,
    file not found, directory listings - EVERYTING is fixed! :D

    Sincerely,
    Lars

    11:10 pm on May 17, 2005 (gmt 0)

    New User

    10+ Year Member

    joined:Feb 22, 2005
    posts:38
    votes: 0


    For some reasons the following code works now:


    RewriteEngine On
    RewriteCond %{REQUEST_URI}!^/sd_
    RewriteCond %{HTTP_HOST}!^www\.example\.com$ [NC]
    RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
    RewriteRule (.*) /sd_%1/$1 [L]

    http:subbie.example.com --> /home/docs/sd_subbie/index.html
    http:subbie.example.com/ --> /home/docs/sd_subbie/index.html

    http:subbie2.example.com --> /home/docs/sd_subbie2/index.html
    http:subbie2.example.com/ --> /home/docs/sd_subbie2/index.html

    But how can i get it to work so i also can put 1 map on the subdomain?

    f.e.

    http:subbie.example.com/images/picture.jpg
    http:subbie.example.com/logo/great.jpg

    I think i only need one map layer. To put images and other things only for that domain. I can also put them on another subdomain f.e.

    http://images.domain/picture.jpg
    http://logo.domain/great.jpg

    But i think some kind of mapping structure on the subdomain itself would come in handy.

    [edited by: jdMorgan at 12:03 am (utc) on May 24, 2005]
    [edit reason] Examplified. [/edit]

    12:14 am on May 18, 2005 (gmt 0)

    Senior Member

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

    joined:Mar 31, 2002
    posts:25430
    votes: 0


    We need to be careful, because I suspect that we have a 'translation problem' with some of the terminology.

    This question is not clear to me:

    But how can i get it to work so i also can put 1 map on the subdomain?

    f.e.

    [subbie.example.com...]
    [subbie.example.com...]

    Specifically, the use of the word 'map'. I'm not sure if you want to add subdirectories to the subdomains, add files to subdomain subdirectories, or rewrite requests for /images and /logo to a common, shared subdirectory for all subdomains. The first two features in this list should work with no code changes at all. The last - making a common directory - would simply need one new RewriteRule.

    If none of these are correct, please try to reword your question... This is probably just another 'server' versus 'webhotel' kind of terminology problem, but I'd like to make sure we answer the question you are asking.

    -----

    Important note: Do not end-anchor the domain name patterns, like


    RewriteCond %{HTTP_HOST} !^www\.example\.co[b]m$[/b] [NC]

    This will cause the Rule to fail if the valid hostname www.example.com:80/ is requested.

    Instead, you can use either:


    RewriteCond %{HTTP_HOST} !^www\.example\.com(:[0-9]{1,5})?$ [NC]

    or just

    RewriteCond %{HTTP_HOST} !^www\.example\.co[b]m[/b] [NC]

    Jim

    [edited by: jdMorgan at 12:04 am (utc) on May 24, 2005]

    9:02 am on May 18, 2005 (gmt 0)

    New User

    10+ Year Member

    joined:Feb 22, 2005
    posts:38
    votes: 0


    Hello, very much thanks for your time. I shall rewrite my question to make things more clear.

    I want to be able to make a map into the subdirectory map and all the map requests must end up in the map they are located. F.e.

    http://sub.example/* --> ../httpdocs/sd_sub/*
    http://sub.example/images/* --> ../httpdocs/sd-sub/images/*

    So for every subdomain there must be a rewrite for the subdomain to end up in a map, and if there are files requested on a map on the subdomain they must end up in a map in the subdomain map.

    The code i now have does the job, but it rewrites as follows:

    http://sub.example.com/* --> ../httpdocs/sd_sub/*
    http://sub.example.com/*/file.jpg --> redirects to: http://sub.example.com/sd_sub/*/file.jpg --> ../httpdocs/sd-sub/images/*

    The file shows up (200 code) but the location http://sub.example.com/sd_sub/images/logo.jpg is not very cool to have in the locationbar.

    [edited by: jdMorgan at 12:06 am (utc) on May 24, 2005]
    [edit reason] Examplified. [/edit]

    2:00 pm on May 18, 2005 (gmt 0)

    Senior Member

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

    joined:Mar 31, 2002
    posts:25430
    votes: 0


    Ah, OK...

    This was a tough question to understand, because the code is correct for what you want.

    But if the browser location bar is changing, that means the you are getting an external redirect. The code I posted does not do external redirects, it does internal rewrites which do not affect the browser address bar. Therefore, some other code is interfering with your internal rewrites, and forcing an external redirect on the rewritten http://sub.domain.com/sd_sub/*/file.jpg URL.

    Look for additional code in httpd.conf, and in /sd_sub/.htaccess (if it exists) that might be doing a redirect.

    Other possibilities:
    If your site does not not use content negotiation, use Options -MultiViews to disable them.
    Check with your host to make sure that UseCanonicalName is off.
    These two are not likely causes, but sometimes cause strange problems problems.

    If your browser address bar changes, you should be seeing a 301 or 302 redirect response. Use the server headers [webmasterworld.com] checker to check your server response to the original request for http://sub.domain.com/*/file.jpg

    Jim

    5:01 pm on May 18, 2005 (gmt 0)

    New User

    10+ Year Member

    joined:Feb 22, 2005
    posts:38
    votes: 0


    There is only one .htaccess on the domain (in the httpdocs directory. UseCanonicalName is off. Can the multiviews cause the slash problem?

    Some things that are happening:

    .htacces code:


    RewriteEngine On
    RewriteCond %{REQUEST_URI} !^/sd_
    RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
    RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
    RewriteRule (.*) /sd_%1/$1 [L]

    http://lekker.example.com/
    display's /httpdocs/sd_lekker/ (200 code)
    locationbar shows http://lekker.example.com/

    http://lekker.example.com/2005
    display's /httpdocs/2005/ (no code)
    locationbar shows httpp://lekker.example.com/sd_lekker/2005/

    http://lekker.example.com/2005/
    display's /httpdocs/sd_lekker/2005/ (200 code)
    locationbar shows httpp://lekker.example.com/2005/

    http://lekker.example.com/2005/image.jpg
    display's /httpdocs/sd_lekker/2005/image.jpg (200 code)
    locationbar shows http://lekker.example.com/2005/image.jpg

    But sometimes this also happens:

    http://lekker.example.com/
    display's /httpdocs/sd_lekker/ (200 code)
    locationbar shows http://lekker.example.com/sd_lekker/

    [edited by: jdMorgan at 12:10 am (utc) on May 24, 2005]
    [edit reason] Examplified. [/edit]

    5:15 pm on May 18, 2005 (gmt 0)

    New User

    10+ Year Member

    joined:Feb 22, 2005
    posts:38
    votes: 0


    Sorry there was a error in the reports posted above the .htaccess generates the following:

    http://lekker.example.com/
    display's /httpdocs/sd_lekker/ (200 code)
    locationbar shows http://lekker.example.com/

    http://lekker.example.com/2005
    display's /httpdocs/sd_lekker/2005/ (no code)
    locationbar shows httpp://lekker.example.com/sd_lekker/2005/

    http://lekker.example.com/2005/
    display's /httpdocs/sd_lekker/2005/ (200 code)
    locationbar shows http://lekker.example.com/2005/

    http://lekker.example.com/2005/image.jpg
    display's /httpdocs/sd_lekker/2005/image.jpg (200 code)
    locationbar shows http://lekker.example.com/2005/image.jpg

    But sometimes this also happens:

    http://lekker.example.com/
    display's /httpdocs/sd_lekker/ (200 code)
    locationbar shows http://lekker.example.com/sd_lekker/

    [edited by: jdMorgan at 12:11 am (utc) on May 24, 2005]
    [edit reason] Examplified. [/edit]

    2:59 pm on May 21, 2005 (gmt 0)

    New User

    10+ Year Member

    joined:Feb 22, 2005
    posts:38
    votes: 0


    Nobody has any clue where the slash problem comes from?
    4:12 pm on May 21, 2005 (gmt 0)

    Senior Member

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

    joined:Mar 31, 2002
    posts:25430
    votes: 0


    Please see message #5 above. If the client includes a port number, your rewrites will fail.

    Jim

    12:57 am on May 22, 2005 (gmt 0)

    New User

    10+ Year Member

    joined:Feb 22, 2005
    posts:38
    votes: 0


    I do not understand. All internet websites are on port 80, and can this cause the problem that the location without the slash at the end will not rewite and with a slash wil rewrite? I understand that if someone tries to connect on www.domain.com:910101 the rewrite wil not work but why will

    [sub.example.com...] will not always rewrite and
    [sub.example.com...] will get correct rewrited?

    [edited by: jdMorgan at 12:12 am (utc) on May 24, 2005]
    [edit reason] Example.com [/edit]

    1:22 am on May 22, 2005 (gmt 0)

    Senior Member

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

    joined:Mar 31, 2002
    posts:25430
    votes: 0


    Try "http://www.example.com:80/" using any Mozilla browser.

    This will cause your rule to fail, because you have end-anchored the host name in the code you posted above. Since the requested host name tested by the RewriteCond will not end with ".com", but rather with ".com:80", the rule will fail.

    Any user-agent that appends a port number -- whether visible to the user or not -- will cause your rule to fail.

    As far as "All internet websites are on port 80", all I can say is, no they are not. https connections (SSL) use port 443 for example, and there is no 'rule' that says you must connect on port 80 and port 80 only. There are many 'home-hosted' sites on port 8080.

    Debugging is a step-wise thing. If you feel you absolutely must end-anchor your hostname, then do it after you get the code working. If you refuse to try fixes suggested by the members here, then we can't help much.

    Jim

    5:20 am on May 23, 2005 (gmt 0)

    New User

    10+ Year Member

    joined:Feb 22, 2005
    posts:38
    votes: 0


    Thanks for your reply, i understand the $ after the domain is not good therefore i have edited the .htaceess code:


    RewriteEngine On
    RewriteCond %{REQUEST_URI}!^/sd_
    RewriteCond %{HTTP_HOST}!^www\.example\.com [NC]
    RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
    RewriteRule (.*) /sd_%1/$1 [L]

    Things are redirected good for the most of the time, but the problem still exists, i do not think this is a port problem. The strange "slash problem":

    http://lekker.example.com/2005 (without the slash) shows the correct page on /httpdocs/sd_lekker/2005/ but the locationbar redirects to http://lekker.example.com/sd_lekker/2005/

    [edited by: jdMorgan at 12:13 am (utc) on May 24, 2005]
    [edit reason] Example.com [/edit]

    5:13 pm on May 23, 2005 (gmt 0)

    Senior Member

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

    joined:Mar 31, 2002
    posts:25430
    votes: 0


    Here is a simple fix to try. It will work as long as all requests contain a period and a filetype (except for index pages named "/"). If your structure does not follow this rule, then you will have to check for "directory exists" on every request, which will really slow down your server.

    # If requested URI does not end in "/"
    RewriteCond $1 !/$
    # and if no "." in URI
    RewriteCond $1 !\.
    # add a trailing slash
    RewriteRule ^(.+)$ /$1/

    Add this above your existing code. Note: There is no '[L]' on the rule, because you *do* want to process the following rules, even if this rule is invoked.

    Jim

    11:47 pm on May 23, 2005 (gmt 0)

    New User

    10+ Year Member

    joined:Feb 22, 2005
    posts:38
    votes: 0


    Thanks very much jMorgan, we are really getting somewhere. I have the following now:

    RewriteEngine On

    RewriteCond $1!/$
    RewriteCond $1!\.
    RewriteRule ^(.+)$ /$1/

    RewriteCond %{REQUEST_URI}!^/sd_
    RewriteCond %{HTTP_HOST}!^www\.example.com [NC]
    RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
    RewriteRule (.*) /sd_%1/$1 [L]

    http://lekker.example.com/2005 shows /httpdocs/sd_lekker/2005/ and it is not redirecting to the http://lekker.example.com/sd_lekker/2005/ anymore.

    Any change to let the visitor get redirected to http://lekker.example.com/2005/ if they type http://lekker.example.com/2005?

    There is another problem rising. If i request http://lekker.example.com/2005/images i get a 2004 but httpdocs/sd_lekker/2005/images/ does exist. Any thoughts about this?

    I find it really hard to understand to have this much code to get a simpel virtual subdomain on the domain itself with .htaccess...

    [edited by: jdMorgan at 12:15 am (utc) on May 24, 2005]
    [edit reason] Examplified. [/edit]

    11:56 pm on May 23, 2005 (gmt 0)

    Senior Member

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

    joined:Mar 31, 2002
    posts:25430
    votes: 0


    It sound like you have MultiViews enabled. If you don't need them, turn them off!

    Options -MultiViews

    This may be the cause of all of your problems... See message 13 above.

    Jim

    1:12 am on May 27, 2005 (gmt 0)

    New User

    10+ Year Member

    joined:Feb 22, 2005
    posts:38
    votes: 0


    The strange redirect in the location bar has stopped thanks to the multiviews option. But i would like to have the following redirect:

    [subdomain.domein.com...] -> [subdomain.domein.com...]
    [subdomain.domein.com...] -> [subdomain.domein.com...]
    [subdomain.domein.com...] -> [subdomain.domein.com...]

    etc. (the slash) unregarding the name on the maps and depth of the maps. Is this also possible? (I really want to have all the functions of a real subdomain.

    4:12 am on May 27, 2005 (gmt 0)

    Senior Member

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

    joined:Mar 31, 2002
    posts:25430
    votes: 0


    In that case, change the first rule from an internal rewrite to an external redirect:

    RewriteCond $1 !/$
    RewriteCond $1 !\.
    RewriteRule ^(.+)$ http://www.example.com/$1/ [R=301,L]

    Jim
    1:36 am on May 30, 2005 (gmt 0)

    New User

    10+ Year Member

    joined:Feb 22, 2005
    posts:38
    votes: 0


    I have this now:

    RewriteEngine On

    Options -MultiViews

    RewriteCond $1!/$
    RewriteCond $1!\.
    RewriteRule ^(.+)$ /$1/ [R=301,L]

    RewriteCond %{REQUEST_URI}!^/sd_
    RewriteCond %{HTTP_HOST}!^www\.domain\.com [NC]
    RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com
    RewriteRule (.*) /sd_%1/$1 [L]

    And it seems to work properly.
    All the maps are nicely redirected to a slash. The locationbar shows very nice url's (not the sd_ anymore). And i can make a subdomain complete without Plesk :)

    If i find anymore buts i'll let you know ;) Thanks!

    3:20 pm on June 16, 2005 (gmt 0)

    New User

    10+ Year Member

    joined:Feb 22, 2005
    posts:38
    votes: 0


    My host has changed RedHat 9 to RedHat Enterprise ES and there is a strange problem:

    [domain.com...] gets redirected to [domain.com...]

    (the second slash).

    Anyone a idea where this problem comes from, my host said to me he doesnt know. Only thing he has edited is a wildcart to cath all the subdomains to get the script above working again. [*.domain.com....]

    5:35 pm on June 16, 2005 (gmt 0)

    Senior Member

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

    joined:Mar 31, 2002
    posts:25430
    votes: 0


    Perhaps you have UseCanonicalName on in httpd.conf, and the host has added a trailing slash to the end of the canonical name.

    Jim

     

    Join The Conversation

    Moderators and Top Contributors

    Hot Threads This Week

    Featured Threads

    Free SEO Tools

    Hire Expert Members