How to selectively redirect?
I have a domain alias that i want to forward to a specific directory on my main site. How do I g开发者_如何学Goet it so that only people visiting the alias "fakesite.com" to be redirected to "mainsite.com/fake" instead of everyone redirected to that?
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
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).
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">
</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.
精彩评论