Forum Moderators: phranque

Message Too Old, No Replies

sub domain to parm rewrite

sub domain to parm rewrite

         

quiet

8:23 am on Nov 3, 2003 (gmt 0)

10+ Year Member



Can someone help me with a rewrite?

I'd like to do something like:

abc.mydomain.tld

to

www.mydomain.tld/index.cgi?a=abc&p=pathdata

with a [R=301]

Something like this?

RewriteCond %{HTTP_HOST} !^www\.mydomain\.tld$
RewriteRule ^http://(.*)\.mydomain.tld(.*) http://www.mydomain.tld/index.cgi?a=$1&p=$2 [R=301]

I think I'd also need to condition to ignore it if it had no subdomain at all...

Thanks tons for any comments.

--q

[edited by: jdMorgan at 1:51 pm (utc) on Nov. 3, 2003]
[edit reason] Delinked [/edit]

jdMorgan

1:58 pm on Nov 3, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



quiet,

Your example code shows some relationship between the requested subdomain and subdirectory and the substitution querystring. But your description of what you want to do does not describe this relationship at all. Since mod_rewrite demands extreme precision, there's no way anyone can give a correct answer because the question is not defined. Please describe how you wish to map requested URLs to destination URLs, and how you think your code does that.

There is one obvious error; the pattern of a RewriteRule cannot contain a domain -- only the requested URI is matched in RewriteRule; Domain names must be matched using RewriteCond.

Ref: Introduction to mod_rewrite [webmasterworld.com]

Jim

quiet

3:56 pm on Nov 3, 2003 (gmt 0)

10+ Year Member



Thanks Jim,

I was afraid you were going to say something like that regarding the domain name and URI. Dag nab it.

I saw an example on apache.org (I've been using their rewrite guide for some very basic redirects) that used the whole URL in the the rule line with a [-F] (it was in the proxy deny section) so I was hoping I could use it to redirect.

What I ultimately want to do is move the sub domain that is being used into the URI as an inquiry string for index.cgi.

I'm trying to clean up a bad linking plan where I haven't had success contacting the administrators of linking sites. In the past non "www" subdomains went to a program that parsed the subdomain and redirected to a cookie setting program on the "www" sub domain that then sent the user to the correct page.

It sounds like you are saying I'm out of luck. There isn't anything that lets you carry matching info from the condition line to the rule line is there? I think I'm really reaching here.

Thanks for your reply!

--q

jdMorgan

4:24 pm on Nov 3, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



quiet,

> There isn't anything that lets you carry matching info from the condition line to the rule line is there?

Yes there is, see RewriteCond [httpd.apache.org] backreferences section. A backreference created by a parenthsized sub-expression in a RewriteCond may be referred to by subsequent RewriteConds and RewriteRules by referring to %(n) where n is a number 1 to 9.

(There is not much you *cannot* do with mod_rewrite, as long as the server variables are available.)

You are looking to do a check of the requested subdomain in %{HTTP_HOST} using a RewriteCond, followed by a back-reference to that subdomain in the following RewriteRule. The RewriteConds can also *require* a subdomain, and a subdomain other than "www" -- otherwise you can use them to stop the RewriteRule from acting.

Something like:

RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com
RewriteRule ^(.*) http://www.example.com/index.cgi?a=%1&p=$1 [R=301,L]

Jim

quiet

5:26 pm on Nov 3, 2003 (gmt 0)

10+ Year Member



Jim,

Absolutely perfect. I missed the % backreference ability.

Thanks so much for your detailed explanations. Huge help!

--q