Forum Moderators: phranque
I've read some of the posts here having to do with redirects/rewrites and I'm still not sure about the following.
I've set up a subdomain and need to redirect it to our main site, but with an affiliate code appended to it. How would I do that? Here's what I have, and I believe it's the last line I need help with but I'm not sure at all. As in a previous post, I would like the url to remain the subdomain name in the browser's address bar.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^test.themaindomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.test.themaindomain.com$
RewriteRule ^(.*)$ [themaindomain.com?PARTNER=michael...] [R=301,L]
In an apache config file, it would be like this:
<VirtualHost 192.168.0.1:80>
ServerName test.themaindomain.com
ServerAlias www.test.themaindomain.com
RewriteEngine On
RewriteRule (.+) [themaindomain.com$1?PARTNER=michael...]
</VirtualHost>
So, I'm hoping someone can tranlsate that to the .htaccess file. I think I'm close in the first code I gave, but in the rewrite part I don't know how to make it the equivalent of +- so that I don't get a domain not found error message since [themaindomain.com?PARTNER=michael...] does not actually exist. It needs to go to the main site but carry the affiliate ID.
Hope I've been able to explain that okay.
Thanks for any help,
meganp
If you want to keep the subdomain showing in the address bar, you cannot do an external redirect. Instead, you must do an internal rewrite. You can do this only if your subdomain resolves to the same filespace as your main domain. It's not clear from your code or from what you posted whether this is the case.
Once a request has gone through DNS and the server config code, and has "landed" in a specific filespace, then "domain" and "subdomain" have no meaning. Instead, you will be "inside" that virtual server's filespace, and can only access the requested domain information using the RewriteCond %{HTTP_HOST} variable or similar variables in PERL or PHP.
The main point is that at some point in the process, a "domain" is resolved to a filespace on a given server. You can do internal rewrites from one "domain" to another as long as they share the same filespace; then the client browser will not be involved, and therefore the address bar won't change.
If you actually have to redirect to a different server to reach that new domain's files, then the client browser must be involved, and there is no way to avoid the URL in the address bar changing, unless you want the first domain's server to act as a proxy for the second domain's server. That adds a lot of additional server load and complexity, and can greatly affect access logging and tracking*, but it can be done if it's the only solution.
* The second server will see all requests as originating at the first server, unless it examines the X-HTTP-FORWARDED-FOR header.
Please describe whether the domains involved in your project resolve to the same server or not. If so, the rest is easy.
Jim
Anyway, yes, fortunately the domains involved in this project do resolve to the same server.
Thank you!
meganp
RewriteEngine on
# IF subdomain or www.subdomain requested
RewriteCond %{HTTP_HOST} ^(www\.)?test\.themaindomain\.com
# AND querystring is blank
RewriteCond %{QUERY_STRING} ^$
# append querystring
RewriteRule ^(.*)$ /$1?PARTNER=michael [L]
As noted above, at this stage, since the domain has resolved to a server, the domain name "disappears" and is irrelevant to the local URL-path we are rewriting to. At this point in the server processing, we are dealing with files, not URLs, and all the files are "right here" in this directory structure.
If you were referring to the Rewrite arbitrary subdomains to subdirectories [webmasterworld.com] thread, then some of that technique may be useful if you need to handle a large number of subdomains and rewrite their requests to specific subdomains or append specific querystrings derived directly from the subdomain name. Otherwise, it probably does not apply.
Jim
When the subdomain is created through cpanel, it creates a new folder and puts a cgi-bin in that subdomain folder. So, that subdomain folder is in the root folder, but it doesn't sound like that's what you mean. I tried adding the .htaccess file to the subdomain folder, but that didn't redirect... I'm assuming because it's not all resolving to the same root folder (?).
The other thread I was referring to is this one:
[webmasterworld.com...]
I guess I'm pretty lost with all this. Looking through the threads on this forum, I had no idea that .htaccess could be such a powerful tool though!
Well, thanks again. I'm sorry to be so dense with this. I realize after reading through the charter (sorry I missed that first when I landed here from Google) that my question probably goes beyond what the forum is for, but I do appreciate that you took the time to help.
Kind regards,
meganp
Jim