Forum Moderators: phranque

Message Too Old, No Replies

Rewrite rule

         

gpattabhi

1:59 am on May 13, 2003 (gmt 0)

10+ Year Member



I am anewbie with private myserver. I have been trying to understand whats going on in my rewrite rule. This is what it is
ServerAlias *.domain.com
RewriteEngine On
RewriteCond %{HTTP_HOST}!www.domain.com [NC]
RewriteCond %{HTTP_HOST} (.*).domain.com [NC]
RewriteRule ^/(.*)$ /home/virtual/site3/fst/home/%1/public_html/$1 [L]

This directs user.domain.com to his user directory. My question is what does ^/(.*)$ in the rewrite rule do.
If the URL was
[user1.domain.com...]
what are the values of %1 and $1
Thanks a bunch in advance.

jdMorgan

2:43 am on May 13, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



gpattabhi,

Welcome to WebmasterWorld [webmasterworld.com]!

RewriteCond %{HTTP_HOST} !www.domain.com [NC] prevents the rewrite from taking place if the requested subdomain is "www".

RewriteCond %{HTTP_HOST} (.*).domain.com [NC] finds the subdomain (username) and places it into the backreference (variable) %1

RewriteRule ^/(.*)$ finds the requested page on your site and puts it into backreference $1

For your example, %1 would be "user1" and $1 would be blank. This will be redirected to [yourdomain.com...] [L]

If the requested URL was [user2.domain.com...] then %1 would be user2 and $1 would be foo.html. This will be redirected to [yourdomain.com...]

By the way, the RewriteConds have errors (relatively harmless, but errors) in them. They should should be:
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com [NC]

Note the backslashes required to escape the periods, which are special characters for the regular expressions used in mod_rewrite, and the start anchors "^" thrown in for good measure.

Ref: Introduction to mod_rewrite [webmasterworld.com]

HTH,
Jim

gpattabhi

3:24 am on May 13, 2003 (gmt 0)

10+ Year Member



wow thanks for the quick reply
-ganesh