Welcome to WebmasterWorld Guest from 54.226.246.160

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

automatically routing subdomains via mod_rewrite?

instead of hand-coding each rule for each sub

     

amznVibe

4:54 am on Jan 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've read a few threads on here about using mod_rewrite
for pseudo subdomains but I am looking to produce a superior
routine that doesn't require hand coding of each sub into .HTACCESS
Need some help from the mod_rewrite Gurus!

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]

amznVibe

1:17 pm on Jan 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Okay I simplified it and now I can see a real problem that
others have documented but never found a solution. Maybe the guru's here can help.
Note I only have access to .HTACCESS (not httpd.conf) but I know mod_rewrite works fine.

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

xlcus

5:13 pm on Jan 12, 2003 (gmt 0)

10+ Year Member



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]

andreasfriedrich

6:11 pm on Jan 12, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



RewriteMap

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


Note: The WebmasterWorld posting software deletes spaces preceding the exclamation point "!" character. It also replaces a solid vertical pipe symbol with a broken vertical pipe "Ķ" symbol. Both of these changes will need to be undone in any code you copy from WebmasterWorld. Make sure to include a space preceding the "!" in mod_rewrite code, and always replace "Ķ" with a solid vertical pipe.

amznVibe

2:29 am on Jan 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thank you both for posting, unfortuantely
neither code set works at all. I will have
to study the 2nd set very carefully to decode it.
But now I understand the logic of how to stop
the recursion so I will try to hack a fix too.

Andreas I am assuming "server" gets replaced
by mydomain.com and not www.mydomain.com

andreasfriedrich

2:45 am on Jan 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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

amznVibe

3:25 am on Jan 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



thanks for your patience...
I am still getting a server 500 error
(and then another 500 on the errordocument)
when I even try just the normal www.mydomain.com

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]

andreasfriedrich

3:41 am on Jan 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I just copied and pasted the code you posted and inserted spaces at the appropriate places and everything worked.

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

jdMorgan

3:57 am on Jan 13, 2003 (gmt 0)

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



Looks like I missed the party!

RewriteCond %2<->%3__SPACE__!^(.*)<->\1$ [NC]

Is this "1$" going to cause a problem? Should it be "$1"?

AF - This one gets the award for the Coolest Rewrite Posted So Far

Jim

amznVibe

4:14 am on Jan 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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)

amznVibe

4:24 am on Jan 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Andreas you are a genious!

The last post worked perfectly, so it was a spacing issue.

This definitely needs to go into the webmasterworld library.

You just saved me alot of work over this year.

Do you have a tip jar somewhere? :)

-aV-

amznVibe

4:59 am on Jan 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



by the way, any idea why it doesn't work with virutal/alias directories?

ie.
mydomain.com/webalizer is an alias and works

webalizer.mydomain.com doesn't work with your code

Just noticed that, though its no biggie.

andreasfriedrich

5:08 am on Jan 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You will need to add a PT flag to the L flag in the RewriteRule.

RewriteRule ^(.+) /%2/$1 [PT,L]

Do you have a tip jar somewhere?

Perhaps you could buy me a pint of cider if we ever meet at one of the WebmasterWorld conferences ;)

Well thanks and glad to help.

Andreas

andreasfriedrich

5:16 am on Jan 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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

amznVibe

5:45 am on Jan 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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?

andreasfriedrich

5:57 am on Jan 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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

jdMorgan

6:19 am on Jan 13, 2003 (gmt 0)

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



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

andreasfriedrich

6:33 am on Jan 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Remember: CondPattern is a standard Extended Regular Expression [httpd.apache.org]

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

andreasfriedrich

6:37 am on Jan 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As a general rule you use $1 or %1 to refer to the first matched subpattern after the rule matched. \1 refers to the first matched subpattern within the same regular expression.

Andreas

jdMorgan

6:48 am on Jan 13, 2003 (gmt 0)

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



I have never tried any backreferences or regex that were not documented within mod_rewrite when writing rewrite rules!

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

jdMorgan

6:49 am on Jan 13, 2003 (gmt 0)

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



Cross-posted...

So, it is unconditionally a "local" backreference within the RewriteCond that includes it?

Jim

andreasfriedrich

7:11 am on Jan 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



%1 would have referred to the first matched subpattern from the previous RewriteCond directive. \1 is indeed local to the current regular expression.

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

jdMorgan

7:19 am on Jan 13, 2003 (gmt 0)

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



Ah, OK!

A new trick to play with. Thanks a lot!

(I will now return this thread to its rightful owner, and apologize for the distraction...) :)

Jim

Torben Lundsgaard

11:54 pm on Feb 18, 2003 (gmt 0)

10+ Year Member



Hi

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

andreasfriedrich

2:48 pm on Feb 19, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



# 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}__SPACE__!^$
# Rewrite only when Host is not main domain
# Delete if there is no main server that is not subject to rewriting
RewriteCond %{HTTP_HOST}__SPACE__!^server$ [NC]
# Extract domain (%1), tld (%2) and first two path elements (%3)
RewriteCond %{HTTP_HOST}<->%{REQUEST_URI} \.([^.]+)\.([^.]+)$<->/([^/]+/[^/]+) [NC]
# Rewrite only when "tld/domain" not equal to first two path elements
RewriteCond %2/%1<->%3__SPACE__!^(.*)<->\1$ [NC]
# Rewrite to /subdomain/request
RewriteRule ^(.+) /%2/%1/$1 [L]

Torben Lundsgaard

3:55 pm on Feb 19, 2003 (gmt 0)

10+ Year Member



Hi Andreas,

Thanks for your help

I'm still having some problems.

Now I don't get any errors but for some the reason it's trying to get the requested file from / rather than /tld/domain/

I replaced the __SPACE__ with a real space

I don't understand <-> What does it mean?

Torben

andreasfriedrich

4:22 pm on Feb 19, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Appending a "?" to this rule might help.

RewriteCond %{HTTP_HOST}<->%{REQUEST_URI} \.([^.]+)\.([^.]+)$<->/([^/]+/[^/]+)? [NC]

andreasfriedrich

4:27 pm on Feb 19, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>>I'm still having some problems.

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.

Torben Lundsgaard

5:28 pm on Feb 19, 2003 (gmt 0)

10+ Year Member



I have only got acces to .htaccess

Torben Lundsgaard

5:45 pm on Feb 19, 2003 (gmt 0)

10+ Year Member



I'm in contact with a very helpful administrator

Will it help to enable RewriteLog? And what RewriteLogLevel is safe to use without slowing down the serve?

This 32 message thread spans 2 pages: 32
 

Featured Threads

Hot Threads This Week

Hot Threads This Month