Forum Moderators: phranque

Message Too Old, No Replies

Another lovely mod_rewrite problem...

Using mod_rewrite with an external map file...

         

Gadnium

8:33 pm on May 21, 2005 (gmt 0)

10+ Year Member



Hello again everyone.

I have a little problem regarding the www.

Here's what I have so far THAT WORKS(Partially)

###
# Map File
###
domain1.com/clients/72504
domain2.com/clients/72505
domain3.com/clients/72506
...

###
# httpd.conf
###

UseCanonicalName Off
RewriteEngine On

RewriteMap lowercase int:tolower
RewriteMap vhost txt:/www/conf/vhosts.map

RewriteCond ${lowercase:%{SERVER_NAME}} ^(.+)$
RewriteCond ${vhost:%1} ^(/.*)$
RewriteRule ^/(.*)$ %1/$1 [L]

This setup works fine when the URL is example1.com. However, it does NOT work with www.example1.com.

I know I could simply add another entry with the www. in my map file like:

domain1.com/clients/72504
www.domain1.com/clients/72504

However, this is unnecessary if I can have apache remove the www automatically.

How can I remove the www portion of the URL in order to match my map file?

Thanx in advance,

Gadnium

jdMorgan

8:46 pm on May 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I haven't used this mapping technique before, but here's a guess:

RewriteCond ${lowercase:%{SERVER_NAME}} [b]^(www\.)?[/b](.+)$
RewriteCond ${vhost[b]:%2}[/b] ^(/.*)$
RewriteRule ^/(.*)$ %1/$1 [L]

Jim

Gadnium

8:54 pm on May 21, 2005 (gmt 0)

10+ Year Member



Thanks but I've already tried that.

This would still require the double entry in the map file because it is matching the entire string with or without the www.

I need to remove the www if present.

Gadnium

jdMorgan

9:40 pm on May 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Nope,

There are two subpatterns there, and only %2 is passed to your vhost: map. "www.", if present, is assigned to %1, and promptly dropped.

Jim

Gadnium

9:57 pm on May 21, 2005 (gmt 0)

10+ Year Member



My Fault..

Your solution works:
RewriteCond ${lowercase:%{SERVER_NAME}} ^(www\.)?(.+)$
RewriteCond ${vhost:%2} ^(/.*)$
RewriteRule ^/(.*)$ %1/$1 [L]

As I did not know the %1 was dropped:
RewriteCond ${lowercase:%{SERVER_NAME}} ^(www\.)?(.+)$
RewriteCond ${vhost:%2} ^(/.*)$
RewriteRule ^/(.*)$ %2/$1 [L]

Thank You.

Gadnium