Forum Moderators: phranque
I've gotten my host to turn on wildcard DNS for me and the follow code definitely works properly:
RewriteCond %{HTTP_HOST} images\.mydomain\.com [NC]
RewriteCond %{REQUEST_URI}!^/images/.*
RewriteRule (.*)/images/$1[L]
So if [images.mydomain.com...] is entered,
it transparently uses [mydomain.com...]
Now to attempt automatic mode, this does NOT work, help!
RewriteMap lowercase int:tolower
RewriteEngine on
RewriteCond ${lowercase:%{HTTP_HOST}}!^$
RewriteCond ${lowercase:%{HTTP_HOST}}!^www\.mydomain.com$
RewriteCond ${lowercase:%{HTTP_HOST}} ^(www\.Ķ)([^.]+)\.mydomain\.com$
RewriteRule ^(.+) ${lowercase:%{HTTP_HOST}}$1 [C]
RewriteRule ^(www\.Ķ)([^.]+)\.mydomain\.com.*) /$2$3 [L]
RewriteEngine on
RewriteCond %{HTTP_HOST}!^$
RewriteCond %{HTTP_HOST}!^www\.mydomain\.com$
RewriteCond %{HTTP_HOST} ^(www\.Ķ)([^.]+)\.mydomain\.com$
RewriteRule ^(.+) %{HTTP_HOST}$1 [C]
RewriteRule ^(www\.Ķ)([^.]+)\.mydomain\.com(.*) /$2/$3 [L]
Here's the error I get when I try to test with:
[images.mydomain.com...]
(in reality error is about 100x longer)
Forbidden
You don't have permission to access /images/images/images/images/images/images/images/
ges/images/images/index.htm/index.htm/images/index.htm/
.htm/images/images/index.htm/index.htm/images/index.htm/
x.htm/images/images/images/index.htm/index.htm/images/
htm/index.htm/images/images/images/images/index.htm/
m/images/tm/index.htm/images/index.htm/index.htm on this server
So if [images.mydomain.com...] is entered,
it transparently uses [mydomain.com...]If you put a rule in to stop the processing when you reach just mydomain.com, it should stop the recursion. Something like the following code might do everything you need... (This is untested, btw)
RewriteEngine on
// If the host is just mydomain.com, do nothing more
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteRule ^.*$ - [L]
// Otherwise strip off everything before mydomain
// And add it to the start of the request
RewriteCond %{HTTP_HOST} ^(.*?)\.(www\.)?mydomain\.com$ [NC]
RewriteRule ^.*$ http://mydomain.com/%1%{REQUEST_URI} [L]
You cannot use the RewriteMap [httpd.apache.org] directive within .htaccess context.
Preventing recursion
To prevent the recursive internal redirects that cause your error we need to check whether the initial rewrite from subdomain to subdirectory has already taken place.
# Turn RewriteEngine on
RewriteEngine On
#
# The rules are processed in the following order: If the RewriteRule matches
# the RewriteCond directives are evaluated in the order they appear. Since
# the RewriteRule will always match eventually even if we just request / the
# RewriteCond will be evaluated.
#
# Rewrite only when Host is not empty
RewriteCond %{HTTP_HOST}!^$
# Rewrite only when Host is not main domain
RewriteCond %{HTTP_HOST}!^server$ [NC]
# Extract subdomain and first path element
RewriteCond %{HTTP_HOST}<->%{REQUEST_URI} ^(www\.)?([^.]+).*<->/([^/]+) [NC]
# Rewrite only when subdomain not equal to first path element
RewriteCond %2<->%3!^(.*)<->\1$ [NC]
# Rewrite to /subdomain/request
RewriteRule ^(.+) /%2/$1 [L]
Andreas
Andreas I am assuming "server" gets replaced
by mydomain.com and not www.mydomain.com
Andreas I am assuming "server" gets replaced
by mydomain.com and not www.mydomain.com
You need to replace it with a regular expression that matches all domain names that you do not want to do any rewriting for. So it should be something like
(www\.)?domain\.tld. neither code set works at all
I tested it on Apache/1.3.27 (Unix) (Red-Hat/Linux) and it worked just fine. There are a couple of other servers using a very similar setup without any problems.
Andreas
this is the exact code I dropped in
(only mydomain was changed)
there are no pipes used so no need for search/replace
I've tried it at both the beginning and ending of
my other mod_rewrite commands (for bad agent filtering)
RewriteEngine On
RewriteCond %{HTTP_HOST}!^$
RewriteCond %{HTTP_HOST}!^(www\.)?mydomain\.com$ [NC]
RewriteCond %{HTTP_HOST}<->%{REQUEST_URI} ^(www\.)?([^.]+).*<->/([^/]+) [NC]
RewriteCond %2<->%3!^(.*)<->\1$ [NC]
RewriteRule ^(.+) /%2/$1 [L]
RewriteEngine On
RewriteCond %{HTTP_HOST}__SPACE__!^$
RewriteCond %{HTTP_HOST}__SPACE__!^(www\.)?mydomain\.com$ [NC]
RewriteCond %{HTTP_HOST}<->%{REQUEST_URI} ^(www\.)?([^.]+).*<->/([^/]+) [NC]
RewriteCond %2<->%3__SPACE__!^(.*)<->\1$ [NC]
RewriteRule ^(.+) /%2/$1 [L]
Itīs a pity that you have no access to httpd to enable rewrite logging.
Andreas
Itīs a pity that you have no access to httpd to enable rewrite logging
actually with the amount of bandwidth and space I am buying from my host as a reseller,
they will do just about anything within reason for me...
I will try the fix with the space and if that fails, I'll ask them to turn it on for me.
(oh and yes, if I can get this to work, it is indeed one of the niftiest mod_rewrites I've seen, ever
- been trying to do this for a week now with various snippets to work around hard coding each and every subdomain)
Looks like I missed the party!
You really did. I thought about waiting until you showed up but then decided to go on without you since I did not want to keep amznVibe waiting. :) She deserved a quick solutions as a thank you for the nice CSS work [webmasterworld.com].
Is this "1$" going to cause a problem? Should it be "$1"?
No Jim, the \1 is a backreference and the $ the end of string anchor.
It is pretty cool indeed. I like it myself ;)
Developing some rules along those lines (somewhat more complex) was the first freelance job I did a couple of years ago. It earned me 100 DM (USD 50). Probably way too little money, but I was glad to have it. ;)
Andreas
You will need to add a PT flag to the L flag
RewriteRule ^(.+) /%2/$1 [PT,L]
Nope, same reaction, I get a 404
Even this simpler model doesn't work though:
RewriteCond %{HTTP_HOST} webalizer\.domain\.com [NC]
RewriteCond %{REQUEST_URI} !^/domain/.*
RewriteRule (.*) /webalizer/$1 [PT,L]
any other ideas I can try?
(consider it TWO pints of cider :) )
OH! I just noticed another problem with posting code here
in addition to the pipe conversion problem. TABS are stripped out.
So when posting code, we must change tabs to spaces.
This is probably what happened with your original code post.
I think this might be a textarea problem?
ie.
mydomain.com/webalizer is an alias
Would you please post the configuration directive you achieve this with. I can then check it out some things on my own server.
(consider it TWO pints of cider :))
Looking forward to that. I believe there was another person (forgot who that was) who wanted to buy me some drinks. ;)
TABS are stripped out.
Among other things ;). The problem was that [t]he WebmasterWorld posting software deletes spaces preceding the exclamation point "!" character. See my note below the original post.
Andreas
No Jim, the \1 is a backreference and the $ the end of string anchor.
I couldn't parse that "\1$" out... I understand $1 and %1 backreferences, of course, but how does that \1 work?... I tried to read it as an escaped 1 followed by an end anchor, but that did not make sense, since numeric characters don't need to be escaped and the rewrite didn't inject a "1" into the path anywhere above that line anyway...?
Where is this documented? It's very interesting!
Jim
From the regex - POSIX 1003.2 regular expressions man page:
Finally, there is one new type of atom, a back reference: `\' followed by a non-zero decimal digit d matches the same sequence of characters matched by the dth parenthesized subexpression
Andreas
So, why did you use this form of backreference? Does it avoid "clearing" or altering "index" of the backreferences (such as %2 used in the Rule) from the preceding RewriteCond?
A whole new variation to study! :)
Jim
I have never tried any backreferences or regex that were not documented within mod_rewrite when writing rewrite rules! [...] A whole new variation to study!
Yeah, thatīs the beauty of using standards and standard libraries in your project.
Andreas
I have got a problem similar to amznVibe but I'm totally new to mod_rewrite so I could use some help.
I have got a lot of virtual domains, which at the moment are redirected with a php script to different paths like this:
[domain1.com...] -> [domain1.com...]
[domain2.com...] -> [domain2.com...]
[domain3.net...] -> [domain3.net...]
I would like the /com/domain1/ stuff to be invisible to the user. Can you guys tell me how to modify the .htaccess in this thread so the domain and tld are the variables?
I already tried using the example in this thread as it is but I'm just getting:
You don't have permission to access /com/domain1/com/domain1/com/domain1/com/domain1/com/domain1/ on this server.
Thanks
Torben
Did you put that code into the httpd file or an .htaccess file. If you tell me I can test the code in my local server.
>>I don't understand <-> What does it mean?
Nothing really. Itīs just there ;). Well since you have nothing to do with my momentarily quarrels with WebmasterWorld Iīll elaborate a bit.
RewriteCond %{HTTP_HOST}<->%{REQUEST_URI} \.([^.]+)\.([^.]+)$<->/([^/]+/[^/]+)? [NC]
will just build a string that may look like www.domain.tld<->/some/path/to/file.html and try to match that string with the reguar expression \.([^.]+)\.([^.]+)$<->/([^/]+/[^/]+)?. The same could be done using two RewriteCond [httpd.apache.org] directives as well.
In
RewriteCond %2/%1<->%3!^(.*)<->\1$ [NC]
the <-> is necessary, well not literally the <-> but some delimter is necessary to test whether whatīs on the left hand side is the same as whatīs on the right hand side of the delimiter. I could have choosen Aaron as well but this time I liked <-> better. I liked the referrence to Perlīs comparison operator <=> as well.