Forum Moderators: phranque

Message Too Old, No Replies

Rewriting subdomains to script as parameter

         

phoenix_fly

10:19 pm on Apr 27, 2005 (gmt 0)

10+ Year Member



Hello everyone!

I´m having some trouble on finding the exact call to mod_rewrite. Can you help me out?

I need to have all the urls that correspond to my vendors (wich will be subdomains, like www.vendorx.mysite.com), translated into a call to a cgi script, with the vendor name as a parameter.

Example:

www.vendorx.mysite.com -> www.mysite.com/cgi-bin/page.cgi?vendor=ven
+dorx

I´m trying this, without success, in my .htaccess:
RewriteEngine on
RewriteRule ^www\.([^.]+)\.mysite\.com www\.mysite\.com/cgi-
+bin/page\.cgi\?vendor=$1

I´ve tried also to specify the target as the absolute path from my host, instead of with the url:
RewriteRule ^www\.([^.]+)\.mysite\.com /home/mysite/cgi-bin/pa
+ge\.cgi\?vendor=$1

... but Apache still doesn´t make it up for me!
Any ideas?

Thanks a lot,

phoenix_fly

jdMorgan

10:55 pm on Apr 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



phoenix_fly,

Welcome to WebmasterWorld!

RewriteRule cannot "see" the hostname; It sees only the local URL-path. You'll need to use


RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.example\.com

to extract the vendor subdomain, and then back-reference it in the subsequent RewriteRule using "%1".

Jim

phoenix_fly

11:14 pm on Apr 27, 2005 (gmt 0)

10+ Year Member



Hey Jim!

Thanks a lot for the fast reply. And for the forum! (I come from perlmonks, and I think here I will find all the answers to the off-topic stuff there).

I´m really null to Apache, can you please give me more details? In fact, the doc showed the example below, and I didn´t understand the first two instructions.

How would be the line where I get the %1 (wouldn´t be $1?) and build the rewrite rule?

Thanks a lot.

André

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.[^.]+\.host\.com$
RewriteRule ^(.+) %{HTTP_HOST}$1 [C]
RewriteRule ^www\.([^.]+)\.host\.com(.*) /home/$1$2

jdMorgan

11:25 pm on Apr 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



André,

Assuming you want to rewrite requests for *all* resources (pages, images, etc.) to the script, it'd look something like this:


RewriteEngine on
RewriteCond %{REQUEST_URI} !/cgi-bin/page\.cgi
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.example\.com
RewriteRule .* /cgi-bin/page.cgi?vendor=%1 [L]

The first RewriteCond prevents an infinite rewriting loop.

See the documentation cited in our forum charter [webmasterworld.com] for more information.

Jim

phoenix_fly

2:25 am on Apr 28, 2005 (gmt 0)

10+ Year Member



Hello Jim,

Thanks! It worked just fine... untill I tried to catch the directory typed and have it sent as a second parameter, like:

www.vendorx.example.com/products
turns to
www.example.com/cgi-bin/page.cgi?vendor=vendorx&section=products

I really read the documentation you mentioned but, as they said it would, this really looks like a beast to understand... couldn´t figure out what´s wrong below:

RewriteEngine on
RewriteCond %{REQUEST_URI}!/cgi-bin/page\.cgi
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.example\.com
RewriteRule /?([a-z]+)$ /home/example/www/cgi-bin/page.cgi?vendor=%1\&section=$1 [L]

This works perfect when I do type the directory, like:

www.vendorx.example.com/products

But when I do not, the regexp just catches "html", whether i insert the slash or not after .com/

I don´t know where it comes from... "html"

Sorry to come you again, Jim

André

phoenix_fly

4:14 pm on May 1, 2005 (gmt 0)

10+ Year Member



Hello Jim and everybody,

I´ve finally got it working with this:

RewriteEngine on
RewriteCond %{REQUEST_URI}!/cgi-bin/page\.cgi
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.example\.com
RewriteRule ^(.+) /home/example/www/cgi-bin/page.cgi?vendor=%1\&section=$1 [L]

The point seems to be that the rule does not affect the entire url, but only the subdirectories path, if any. If none, apache puts in there "/index.html", so this is why I was getting an unexpected "html" in my last regexp comming out of nowhere.

Please correct me if I said something wrong. this mod_rewrite still seems black magic for me... (hey, the docs could be more friendly don´t you guys think?)

Cheers

phoenix_fly