Forum Moderators: phranque
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]
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]
Jim
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]
Two possibilities are:
There could also be some other problems -- these are just the most likely ones.
Jim
# 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]
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
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
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]
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]
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]
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
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]
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]
[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]
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
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]
# If requested URI does not end in "/"
RewriteCond $1 !/$
# and if no "." in URI
RewriteCond $1 !\.
# add a trailing slash
RewriteRule ^(.+)$ /$1/
Jim
RewriteEngine OnRewriteCond $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]
[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.
RewriteEngine OnOptions -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!
[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....]