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

This 32 message thread spans 2 pages: 32 ( [1] 2 > >     
automatically routing subdomains via mod_rewrite?
instead of hand-coding each rule for each sub
amznVibe




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

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




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

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




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

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




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

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




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

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




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

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




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

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




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

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




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

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




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

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




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

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




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

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




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

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




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

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




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

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




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

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




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

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




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

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




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

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




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

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




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

Cross-posted...

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

Jim

andreasfriedrich




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

%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




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

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




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

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




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

# 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




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

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




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

Appending a "?" to this rule might help.

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

andreasfriedrich




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

>>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




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

I have only got acces to .htaccess

Torben Lundsgaard




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

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 ( [1] 2 > >
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