Forum Moderators: phranque

Message Too Old, No Replies

.htaccess change to give special file from certain domain requests

Close, but not there yet

         

Jeremy_H

3:42 am on Dec 18, 2006 (gmt 0)

10+ Year Member



Hello, I'm trying to modify a line in my .htaccess file.

I have my domain name point to my hosting account, and several other domain names point to the same files. These other domain names do not redirect the users to the first domain, but loads the same content, just under their own domain name.

What I'm trying to do is to set up a rule in my .htaccess file that says if the request is from www.mymaindomain.com then serve them the file they are requesting. If not, give them the file name they requested, but that just has the contents of file alternatedomain.php.

I am using the following code for that:

RewriteCond %{HTTP_HOST}!^www\.domain\.com
RewriteRule (.*) [domain.com...] [L,R=301]

On the alternatedomain.php page, I capture the original domain name for my reports, and then 301 them to the file they wanted but on the main domain.

The problem is, the script uses a redirect to send them to the alternate domain page. I don't want that! I would like to modify the code so if the user requests a page that is not www.domain.com, then they get the page name the requested, but with the contents of the alternatedomain.php page.

To me, it would seem as simple as changing or removing the R=301 part, but that doesn't seem to work:

RewriteCond %{HTTP_HOST}!^www\.domain\.com
RewriteRule (.*) [domain.com...] [L]

Any ideas?

Thanks

jdMorgan

4:37 am on Dec 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Remove the protocol and domain from the substitution URL, and add a RewriteCond to prevent internal rewrite looping:

RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond $1 !^alternatedomain\.php$
RewriteRule (.*) /alternatedomain.php?url=$1 [L]

Assuming that alternatedomain.php is accessible in the same filesystem as the other pages in example.com, and that there are no other complications no mentioned above, that should do it.

Beware of a possibly-unexpected behaviour here. Unless the code above is preceded by another ruleset to canonicalize the domain to www.example.com, then requests to example.com (no "www.") will be rewritten to alternatedomain.php. If you don't want that behaviour and you also don't don't want to redirect non-www to www, then you can modify the pattern in the first RewriteCond above to "!^(www\.)?example\.com". With that change, the rule will then rewrite requests to any domains *except* www.example.com or example.com to alternatedomain.php.

Just a comment:

I have my domain name point to my hosting account, and several other domain names point to the same files. These other domain names do not redirect the users to the first domain, but loads the same content, just under their own domain name.

This is really not a good idea, unless you take steps to keep all but one domain out of the search engines. If you're not doing that already, try a search on WebmasterWorld for "duplicate content". Or see one particularly-well-titled recent thread which discusses this and related issues -- "Duplicate Content - Get it right or perish [webmasterworld.com]".

I'm mentioning this for your benefit, and for the benefit of those who may read this thread later.

Jim

Jeremy_H

5:17 am on Dec 18, 2006 (gmt 0)

10+ Year Member



Thank you, the tweak works perfect.

The reason I am doing this is so I can buy typos of my domain name and redirect them to the correct site.

By associating them with the same hosting account, I save on hosting fees other places would charge me if I wanted to use 301 redirects instead of 302. It also allows me to "convert" all requests perfectly from one domain to the other.

By passing all the traffic to one file, I can then log each request from the misspellings to see which misspelled domain people are coming from, and see if I should or should not repurchase the domain name in the future.

I also can point all non www requests to the appropriate www request using a 301 redirect and choose to track that if I want.

Thanks again.

jdMorgan

5:50 am on Dec 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Notwithstanding the reasons presented above, a 301 redirect from each type-in domain and all their www- and non-www variants to the canonical domain is the only way to prevent problems. Also, make sure that no steps are taken to promote anything but the one canonical domain.

Using typo-domains and type-ins to capture traffic isn't a problem. But if you give search engines any reason to believe that you're attempting to promote more than one URL per unique page, you'll eventually have trouble -- either technical trouble like "split pagerank or link-pop" when only a few URL-variants exist, or major trouble, like getting banned for trying to stuff the index with dozens/hundreds of copies of just a few pages.

A 301 clearly tells the engines that you don't want to be listed by anything but your canonical domain, whereas a 302 is asking them to keep the dozens or hundreds of typos/type-ins, and to associate them all with just a few unique pages. The SEs call that index spamming, and the end result is Not Good.

It's pretty clear their policy is, "One page, one URL."

I don't see any reason why a 301 should cost anything -- If all those domains resolve to your server, it shouldn't be a problem. If you're using "domain forwarding," and the forwarders don't support using 301-permanent redirects, then I suggest getting rid of that and 'doing it right' -- The total traffic is the same either way, so it shouldn't cost more (with the right host) other than a reasonable one-time setup fee. Bottom line is that a typo/type-in domain that won't pay its own registration and incremental hosting costs is a waste of your time.

OK, enough of that non-Apache-related diversion, I'm glad the modded code worked for you!

Jim