tags:

views:

64

answers:

3

I have a domain alias that i want to forward to a specific directory on my main site. How do I get it so that only people visiting the alias "fakesite.com" to be redirected to "mainsite.com/fake" instead of everyone redirected to that?

+1  A: 

If you're using Apache, set up fakesite.com as a virtual host and configure .htaccess on your fakesite.com:

Redirect / http://mainsite.com/fake
stillstanding
+1  A: 

There's really no way to do exactly this in pure HTML (as in: meta tags or anything like that). You could try checking the location in JavaScript (check whether the location is relative to the fake domain and rewrite it, that'll redirect the browser), though, to redirect the client if they have JavaScript enabled.

However you can easily do this if you have Apache by using mod_rewrite.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*\.)?fakesite\.com$ [NC]
RewriteRule ^/(.*)$ http://www.mainsite.com/fake/$1 [L,R=301]

You can put this in your vhost definition file (sites-* directory). Of course it will only work if the domain currently resolves to the IP the vhost runs on.

You can also further customise this rule (there are several good tutorials for how to write rewrite rules), e.g. to preserve the fake URL in the browser rather than sending a 301 redirect code with the "new" location.

If you have PHP available on the fake site's server, you can just rewrite all incoming requests to a single script on that script that sends the following header to the client:

<?php header('Location: http://www.newsite.com/fake'.$_SERVER['REQUEST_URI']); ?>

Otherwise (as pointed out by Pekka) a meta-refresh in HTML is of course a last resort. Redirecting to the new root rather than the requested page on the new server is bad form and will do bad things to your visitor count (confused visitors are unhappy visitors) and Google page rank, though.

If you are talking about aliasing rather than redirecting (i.e. provide the content of the directory on the main site be served to users accessing the fake site), though, you can simply create a vhost for the fake domain (same requirements apply as with the mod_rewrite example) and set the document root to a directory in the other domain's document root. This won't work if the two domains are on different servers, obviously.

If you don't actually own the main site or don't have access to it, you could probably set up a script (e.g. PHP) to set the source of an HTML iframe to the real URL and just show a page with that iframe in it; or for a cleaner copy: read the requested content from the real server in the script and output that content (you'd have to correct all URLs in the HTML you loaded, though, if you want to keep the user on the fake site).

Just be mindful that this kind of "proxying" may violate the other site's Terms of Service (or they may just ban your server's IP).

Alan
A: 

Rewriting the URL is always the better option when available, but I don't see why a meta redirect, put on fakesite.com's index.html, wouldn't work:

<html>
<head>
<title>Add title here</title>
<meta http-equiv="refresh" content="0;url=http://www.mainsite.com/fake"&gt;
</head>
<body>
some content
</body>
</html>

the beauty of this method can be disputed (be sure to also equip the page with a redirect message and a link to mainsite.com/fake to click manually) but it should work nevertheless.

Pekka
There are a couple of problems with this. For one thing it breaks all existing URLs for `fakesite.com` (by either resolving to a `404` or serving the same meta-redirect to all requests). You're basically committing SEO suicide that way. The other problem is that a meta refresh is only triggered after the page has arrived at the browser, if it is executed at all (browsers can't be relied to do this automatically and a redirect intermission page always feels unprofessional). Also it doesn't help if you want to keep the fake URL visible (see: proxying).
Alan
@Alan you are supposing SEO is an issue here, and the domain was used with a full URL structure before, which both doesn't have to be the case. As I said, rewriting is always the better option, but if it's not available, a redirect is the only thing left.
Pekka
SEO is always an issue with websites. If you let all your links rot, not only are you confusing (i.e. turning away) a lot of visitors, you also need to wait for the new links to gain decent ground. If SEO were the most important consideration, the domain name wouldn't change at all. Also, server-side scripting languages are sufficiently widespread that it's a safe assumption that they can be used to create redirects (via `Location` header, not intermission pages) if modifying the vhosts is not an option.
Alan
@Alan the man is asking for how to make a simple redirect for a domain. That domain may be new, and *entirely irrelevant* from an SEO perspective, in which case a stupid meta redirect can be perfectly fine. We all agree on how rewriting is the much better option, so let him make his choice.
Pekka
@Pekka I'm not saying it's not a valid, workable last resort. I'm just saying that it's highly probable there are better ways to do this than to catch all incoming requests on the old site and forward them to a possibly irrelevant page on the new site. Apart from that you would need to set up your server to redirect all request to the redirect page in the first place (in Apache probably as simple as defining it as the 404 page in a `.htaccess`).
Alan