Freedom
7th November 2004, 04:54 AM
If I have domaina.com/forum
and I move to domainb.com
How should I make my .htaccess for domaina.com to redirect urls from domaina.com/forum/showthread.php?etc .. to domainb.com correctly redirect?
Floris
19th November 2004, 05:56 AM
If I have domaina.com/forum
and I move to domainb.com
How should I make my .htaccess for domaina.com to redirect urls from domaina.com/forum/showthread.php?etc .. to domainb.com correctly redirect?I never really mastered this myself, I hope someone can explain this in detail to noobs lke us :p :cow:
buro9
28th November 2004, 12:56 PM
There are two ways to do this... the first is to use htaccess and error pages, and the second is to use mod_rewrite.
The first option:
On domaina.com create a .htaccess file and within it put this:
ErrorDocument 404 /redirect.php
That .htaccess file should be in your root folder... i.e. if a file name index.htm gets displayed at http://www.domaina.com/index.htm then the folder that index.htm is in is your root folder from the web servers perspective.
Then create the .php file redirect.php and place it in your root folder too.
Within that file you want to create your redirect... note that you have access to one very important piece of information: $REQUEST_URI contains the local path to the file that was requested... so that is what you want to use:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Redirecting, please wait...</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Refresh" CONTENT="0; URL=http://www.domainb.com<?php echo $REQUEST_URI ?>">
<script type="text/javascript">
function redirect() {
this.location.href = "http://www.domainb.com<?php echo $REQUEST_URI ?>";
}
</script>
</head>
<body onload="redirect();">
Redirecting... if you are still here in a few seconds then
<a href="<?php echo "http://www.domainb.com".$REQUEST_URI ?>">click here</a>.
</body>
</html>
I've put 3 redirects in there for you, a META one that all broswers should honour... a JavaScript one in case the META one is not obeyed... and a manual link in case someone has a bizarre browser and has turned off redirects.
Now... the fun bit... delete EVERYTHING from domaina.com that is not those two files.
What happens now is this, a request gets made for /forum/index.php, that file doesn't exist so it gets trapped by the 404 error, the 404 handler obeys the .htaccess file and passes the error to the error handler... the error handler is the redirect.php document which now sends the user to the new domain :D
Limitations: Requests for images and other content types don't get redirected... and your error logs get big ;)
Advantages: If you can run .htaccess then this works... which means it works almost everywhere.
The second option:
Better than the above is mod_rewrite. Not all servers have mod_rewrite enabled, so this is not something that everyone can use.
It is better though as it works on every item, images and everything, and can return 301 headers (moved permanently) which will keep spiders from Google going to the correct destination across the domains.
To use mod_rewrite you should first look at your phpinfo to see whether you have it... let's look at mine:
http://www.bowlie.com/phpinfo.php
In the Apache section, under Loaded Modules you can see mod_rewrite. If you don't see that on yours, then you cannot use this and you need to use the option 1 above. I am not going to get into how to compile and load a module ;)
First... documentation on mod_rewrite can be found here:
http://httpd.apache.org/docs/mod/mod_rewrite.html
Now, create your .htaccess document again and this time make the contents this:
RewriteEngine on
RewriteRule ^/~(.+) http://www.domainb.com/~$1 [R=301,L]
Explaining that...
RewriteEngine on this turns on the mod_rewrite engine.
RewriteRule ^/~(.+) http://www.domainb.com/~$1 [R=301,L]
4 parts:
RewriteRule = What to perform
^/~(.+) = Regular expression, when to perform it
http://www.domainb.com/~$1 = What to do when performing it
[R=301,L] = Options when performing it
We effectively matched all requests, take that matched URL and paste it into a new URL... which we then return a redirect header with a HTTP 301 (http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html) (moved permanently).
This is a permanant and invisible redirect... there is no intermediate screen that loads, and images and javascript and ALL other files are redirected too.
Now go delete everything from domaina.com :D
mod_rewrite is the way to go if you can use it... there would be no error logs ever -growing, Google spiders would follow the redirects, images get redirected, etc.
The only problem is that not everyone has mod_rewrite! :)
Note:
All of the above only needs doing on the server that is domaina.com... domainb.com merely receives the new requests.
buro9
28th November 2004, 01:04 PM
Can't you just tell that I'm bored whilst waiting for vBulletin.com to come back up ;)