Click to See Complete Forum and Search --> : mod_rewrite problem


ckevin
12-02-2000, 06:13 AM
I have just hardly setup the Apache 1.3.14 in RedHat 6.2 and wish to use mod_rewrite feature... What I wish to do is

anyuser.MY.com --> /home/MY/user/anyuser
Dir.anyuser.MY.com --> /home/MY/user/anyuser/Dir

And here is the lines needed in httpd.conf:

# Rewritelog logs/rewrite.log
# RewritelogLevel 9

RewriteMap lowercase int:tolower
RewriteEngine on

RewriteCond ${lowercase:%{HTTP_HOST}} !^$
RewriteCond ${lowercase:%{HTTP_HOST}} !^www\.MY\.com$

RewriteCond ${lowercase:%{HTTP_HOST}} ^www\.[^.]+\.MY\.com$
RewriteRule ^(.+) ${lowercase:%{HTTP_HOST}}$1 [C]
RewriteRule ^www\.([^.]+)\.MY\.com(.*) /user/$2$3 [L]

RewriteCond ${lowercase:%{HTTP_HOST}} ^www\.[^.]+\.[^.]+\.MY\.com$
RewriteRule ^(.+) ${lowercase:%{HTTP_HOST}}$1 [C]
RewriteRule ^www\.([^.]+)\.([^.]+)\.MY\.com(.*) /user/$3/$2$4 [L]

However, I run "apachectl configtest" and it said ok but I can't get what I need...

I have setup WildCard (*.MY.com) in DNS and *.MY.com alias with MY.com in Apache, when I type:
http://kevin.MY.com

It only displays the main page of MY.com but not /home/MY/user/kevin!

Please help! I have tried many times but failed...


kevin

freebsd
12-02-2000, 09:34 AM
I don't know if this helps but try it anyway:

RewriteEngine on
RewriteMap lc int:tolower
RewriteCond %{HTTP_HOST} ^$ [NC]
RewriteRule ^(.*) - [L]
RewriteCond %{HTTP_HOST} ^www\.MY\.com$ [NC]
RewriteRule ^(.+) - [L]
RewriteCond %{HTTP_HOST} ^www\.[^.]+\.MY\.com$ [NC]
RewriteRule ^(.+) %{HTTP_HOST}=$1 [C]
RewriteRule ^(.*)=(.*) ${lc:$1}$2 [C]
RewriteRule ^www\.([^.]+)\.MY\.com(.*) /home/MY/user/$1$2 [L]
RewriteCond %{HTTP_HOST} ^www\.[^.]+\.[^.]+\.MY\.com$
RewriteRule ^www\.([^.]+)\.([^.]+)\.MY\.com(.*) /home/MY/user/${lc:$2}/$1$3 [L]

Note: I didn't lc it initially just to conserve resources since I don't think you even need to do that at the beginning.

>>RewriteCond ${lowercase:%{HTTP_HOST}} !^www\.MY\.com$

This line is redundant since http://www.MY.com has uppercase already.

>>I run "apachectl configtest" and it said ok but I can't get what I need.

Because your $3/$2$4 was wrong. There isn't any $4.

If I understood your situation correctly, your domain is always in uppercase while username alias from URL part is case-insensitive but the username in the file system part MUST be lowercase.

[This message has been edited by freebsd (edited 02 December 2000).]

ckevin
12-02-2000, 10:12 AM
thank you freebsd,

sorry, but the result is still the same...

when kevin.MY.com, it still go to the main page of MY.com......

Actually, the MY.com is using the Name-based hosting, so if not alias *.MY.com with MY.com in Apache, the kevin.MY.com would go to the general page of that name-bsaed IP only.

Thanks,

kevin

freebsd
12-02-2000, 04:06 PM
I'm a bit confused now, why don't you post the NameVirtualHost line and the
<VirtualHost> block here so we can take a look at it.

ckevin
12-03-2000, 01:45 AM
sorry for the confuse...

What I have done for:

(1) in DNS, wildcard on, both *.imazn.com and imazn.com have a A record to the same IP 1.1.1.1

(2) In httpd.conf:

NameVirtualHost 1.1.1.1

and my record for imazn.com:

<VirtualHost 1.1.1.1>
DocumentRoot /home/imazn/www
ServerName www.imazn.com (http://www.imazn.com)
ServerAdmin webmaster@imazn.com
ServerAlias imazn.com

RewriteMap lowercase int:tolower
RewriteEngine on

RewriteCond ${lowercase:%{HTTP_HOST}} !^$
RewriteCond ${lowercase:%{HTTP_HOST}} !^www\.imazn\.com$

RewriteCond ${lowercase:%{HTTP_HOST}} ^www\.[^.]+\.imazn\.com$
RewriteRule ^(.+) ${lowercase:%{HTTP_HOST}}$1 [C]
RewriteRule ^www\.([^.]+)\.imazn\.com(.*) /user/$2$3 [L]

RewriteCond ${lowercase:%{HTTP_HOST}} ^www\.[^.]+\.[^.]+\.imazn\.com$
RewriteRule ^(.+) ${lowercase:%{HTTP_HOST}}$1 [C]
RewriteRule ^www\.([^.]+)\.([^.]+)\.imazn\.com(.*) /user/$3/$2$4 [L]

</VirtualHost>

Do you think it is ok? I tried putting the mod_rewrite outside the <VirtualHost> tag but the result seems the same...

Thanks,

kevin

[This message has been edited by ckevin (edited 03 December 2000).]

freebsd
12-03-2000, 06:26 AM
>>Do you think it is ok?

Not quite.

Your Rewrite stuffs should go first (above your VirtualHost block)

Your VirtualHost block should look something like so:
<VirtualHost 1.1.1.1>
RewriteEngine on
RewriteOptions inherit
DocumentRoot /home/imazn/www
ServerName www.imazn.com (http://www.imazn.com)
ServerAdmin webmaster@imazn.com
ServerAlias imazn.com
</VirtualHost>

ckevin
12-03-2000, 11:59 AM
freebsd,

thank you very much for your advice... I have checked for my and your mod_write scripts but still, both fail, it (kevin.imazn.com) does not display the page at "/home/imazn/user/kevin".

still have any idea? i don't want to give up...

really thanks, http://www.linuxnewbie.org/ubb/smile.gif

kevin

freebsd
12-04-2000, 06:16 AM
>>i don't want to give up

Then fix the problem with your rules first, it won't work with the following in the first place:

RewriteRule ^www\.([^.]+)\.imazn\.com(.*) /user/$2$3 [L]

and

RewriteRule ^www\.([^.]+)\.([^.]+)\.imazn\.com(.*) /user/$3/$2$4 [L]

Also, if you don't want to start out with my codes, that is your choice. I don't think you understand the difference comparing yours and mine anyway.

My final suggestion, you should not use false condition all the time:

RewriteCond ${lowercase:%{HTTP_HOST}} !^$

In addition, don't use lowercase for your first condition, it's redundant, you are checking whether or not HTTP_HOST is blank, definitely useless to check for the case.

Why don't you enable the RewriteLog line (for debugging purpose only) and see what happen.

Good Luck!

ckevin
12-04-2000, 08:19 AM
I have put your mod_rewrite scripts and enable the log:

In httpd.conf:

<VirtualHost 1.1.1.1>
Rewritelog logs/rewrite.log
RewritelogLevel 9
RewriteEngine on
RewriteMap lc int:tolower
RewriteCond %{HTTP_HOST} ^$ [NC]
RewriteRule ^(.*) - [L]
RewriteCond %{HTTP_HOST} ^www\.imazn\.com$ [NC]
RewriteRule ^(.+) - [L]
RewriteCond %{HTTP_HOST} ^www\.[^.]+\.imazn\.com$ [NC]
RewriteRule ^(.+) %{HTTP_HOST}=$1 [C]
RewriteRule ^(.*)=(.*) ${lc:$1}$2 [C]
RewriteRule ^www\.([^.]+)\.imazn\.com(.*) /home/imazn/www/user/$1$2 [L]
RewriteCond %{HTTP_HOST} ^www\.[^.]+\.[^.]+\.imazn\.com$
RewriteRule ^www\.([^.]+)\.([^.]+)\.imazn\.com(.*) /home/imazn/www/user/${lc:$2}/$1$3 [L]
DocumentRoot /home/imazn/www
ServerName www.imazn.com (http://www.imazn.com)
ServerAdmin webmaster@imazn.com
ServerAlias imazn.com
</VirtualHost>


The log file said:

216.10.178.10 - - [04/Dec/2000:08:11:03 -0500] [www.imazn.com/sid#812cf78][rid#80ee4d8/initial]
(2) init rewrite engine with requested uri /snow.gif


216.10.178.10 - - [04/Dec/2000:08:11:03 -0500] [www.imazn.com/sid#812cf78][rid#80ee4d8/initial]
(3) applying pattern '^(.*)' to uri '/snow.gif'


216.10.178.10 - - [04/Dec/2000:08:11:03 -0500] [www.imazn.com/sid#812cf78][rid#80ee4d8/initial]
(4) RewriteCond: input='www.imazn.com' pattern='^$' => not-matched


216.10.178.10 - - [04/Dec/2000:08:11:03 -0500] [www.imazn.com/sid#812cf78][rid#80ee4d8/initial]
(3) applying pattern '^(.+)' to uri '/snow.gif'


216.10.178.10 - - [04/Dec/2000:08:11:03 -0500] [www.imazn.com/sid#812cf78][rid#80ee4d8/initial]
(4) RewriteCond: input='www.imazn.com' pattern='^www\.imazn\.com$' => matched


216.10.178.10 - - [04/Dec/2000:08:11:03 -0500] [www.imazn.com/sid#812cf78][rid#80ee4d8/initial]
(1) pass through /snow.gif


202.158.27.95 - - [04/Dec/2000:08:18:50 -0500] [www.imazn.com/sid#812cf78][rid#80ee4d8/initial]
(2) init rewrite engine with requested uri /snow.gif


202.158.27.95 - - [04/Dec/2000:08:18:50 -0500] [www.imazn.com/sid#812cf78][rid#80ee4d8/initial]
(3) applying pattern '^(.*)' to uri '/snow.gif'


202.158.27.95 - - [04/Dec/2000:08:18:50 -0500] [www.imazn.com/sid#812cf78][rid#80ee4d8/initial]
(4) RewriteCond: input='www.imazn.com' pattern='^$' => not-matched


202.158.27.95 - - [04/Dec/2000:08:18:50 -0500] [www.imazn.com/sid#812cf78][rid#80ee4d8/initial]
(3) applying pattern '^(.+)' to uri '/snow.gif'


202.158.27.95 - - [04/Dec/2000:08:18:50 -0500] [www.imazn.com/sid#812cf78][rid#80ee4d8/initial]
(4) RewriteCond: input='www.imazn.com' pattern='^www\.imazn\.com$' => matched


202.158.27.95 - - [04/Dec/2000:08:18:50 -0500] [www.imazn.com/sid#812cf78][rid#80ee4d8/initial]
(1) pass through /snow.gif


I hope you can help me to figure the problem... thanks a lot.

kevin

[This message has been edited by ckevin (edited 04 December 2000).]

freebsd
12-05-2000, 12:06 PM
What URL did you try? It doesn't seem to get to the 3rd condition at all. Still, the problem could be something else instead of mod_rewrite at all.

ckevin
12-05-2000, 09:40 PM
I set the testing URL
http://freshwoman.imazn.com

do you think I need to set:

ServerAlias imazn.com *.imazn.com or just

ServerAlias imazn.com is ok?

The only difference is if set the wild card on, the *.imazn.com would display the main page of imazn.com....

Thanks,

kevin

freebsd
12-06-2000, 06:37 AM
Try to remove ServerAlias line completely.

ckevin
12-06-2000, 07:51 AM
I have removed the Alias but it still can't work...

Thanks,

kevin