A Quick question regarding website redirection

This will be your index.html file:

Code:
<html>
<head>
<title>Redirecting...</title>
<script language="JavaScript">
self.location.href='http://www.website.com';
</script>
</head>
<body>
</body>
</html>

Example


edit: although I don't think the Head and Body are necessary. Never really looked into it.
 
Code:
<head>
<title>Redirect</title>
<meta HTTP-EQUIV="REFRESH" content="2; url=http://www.website.com">
</head>

Another way to do it, this way will not leave out those people who browse with scripts disabled for security reasons.

You can change the "content="2;" to any number you want, depending on how long you want the visitor to see the redirected page. This is 2 seconds btw.
 
Gaverick said:
Another way to do it, this way will not leave out those people who browse with scripts disabled for security reasons.
What about people who disable meta-refreshes, or use a user-agent that doesn't support/allow them? Both methods also have negative impact with search engines. The javascript redirection will not work with spiders, and the latter is often a trick of spammers (gateway pages) and so can trigger penalties.

best to do it serverside, using a 301 (Moved Permanently) redirect. Very simple on Apache-based hosting, just add
Code:
RedirectPermanent /oldpage.htm http://www.example.com/newpage.htm
to an .htaccess file and plop it in the root directory of your site. Note that the location you're redirecting to (the second URL) must be the complete http:// address.

Ref: http://httpd.apache.org/docs/2.0/mod/mod_alias.html#redirectpermanent
 
Last edited:
Augmented said:
best to do it serverside, using a 301 (Moved Permanently) redirect. Very simple on Apache-based hosting...
This sounds like a winning plan to me. Now Aug-dude, please explain it to the clueless (like me)...

You're saying that we just create a file and plop it in the root, file named ".htaccess" -- oldpage.htm I'm guessing should be the index.htm ? The new location I understand. So a browser will automatically look for .htaccess before index.htm? I never did understand server-side instructions regarding page access. :(
 
The .htaccess is for Apache so when a request comes in for pageX it'll check the .htaccess and see what it says :) You can do lots of funky stuff in .htaccess files.

JS redirect is *very* bad and anyone who relies on it in their websites should be shot.
 
Raist said:
You're saying that we just create a file and plop it in the root, file named ".htaccess" -- oldpage.htm I'm guessing should be the index.htm ?
Yes. If you want to redirect /index.htm to http://www.your-site.com/home.php, for example, then your rule would be:
Code:
RedirectPermanent /index.htm http://www.your-site.com/home.php
The file must be called only '.htaccess'. Windows Notepad has a tendency to silently add a .txt onto the end of the filename. So make sure that it's not there when you upload it.

The new location I understand. So a browser will automatically look for .htaccess before index.htm? I never did understand server-side instructions regarding page access. :(
.htaccess files are never seen by the browser, it all happens on the server-side. When a server receives a request for a resource (like index.htm or header.jpg), it checks several places for anything that might need doing to that request. This can be in the main server config, the virtualhost definition for the individual site or an .htaccess in the site directories.

So, the user-agent/browser requests a page on a server. The server finds out that the page request should be redirected to another location, and sends that response back to the user-agent. The user-agent now must (if it adheres to the HTTP specification) automatically request the page at the new location. And in the case of the 301 redirect, it must do this for all future requests. Thus the server receives the new request and can send back all the data for the page.

Be aware that .htaccess files operate recursively. So a 'rule' applied in a .htaccess file in www.123.com/dir1/ will also be applied to any sub-directories like www.123.com/dir1/dir2. You can override this behaviour by placing another .htaccess in the sub-directory to cancel the one higher up.

More on htaccess here: http://httpd.apache.org/docs/1.3/howto/htaccess.html

:)
 
Back
Top Bottom