Web Hosting Talk







View Full Version : Redirecting (server side?)


viol
05-13-2002, 12:03 AM
When I want to redirect a page to another one, inside my site, I always use the meta "refresh" tag. But I have noticed that some sites can redirect using a smarter way, without taking the visitor to the old page. The page is already loaded with the new address.
How can I do that?
Is it some server side instruction?
What language is it written?
Any "learn by example" sample?
Thanks a lot.

m6.net
05-13-2002, 12:12 AM
Hi viol,

You can use ASP codes to perform this task. Here is the sample codes:

<%
HostHeader = LCase(Request.ServerVariables("HTTP_HOST"))
Scriptname = LCase(Request.ServerVariables("SCRIPT_NAME"))

'workaround here, if no www can be found just add that since that is how
'the url in the database looks like and hence the lookup will work
if instr(1,hostheader,"www") = 0 then
hostheader = "www." & hostheader
end if

'Response.write hostheader

if (session("SiteID")="" or Session("HostHeader")<>HostHeader) then
session("SiteID") = HostLookUp(HostHeader)
session("HostHeader") = Replace(HostHeader,"www.","")
end if

'response.write("hostheader="&hostheader)

Function HostLookUp(HostHeader)
Set conn = Server.CreateObject("ADODB.Connection")
conn.open Application("DB_DSN")
set objRS = CreateObject("ADODB.Recordset")
TempHost = 0
'Looking for active sites
sql = "SELECT Site.SiteID, Site.SiteTitle, Site.SiteEmail, Site.HostHeader, site.Description, site.sitehead FROM Site WHERE (Site.HostHeader LIKE 'http://" & HostHeader & "') "
objRS.Open sql, conn
if not (objRS.EOF or objRS.BOF) then
TempHost = objRS.Fields(0).Value
session("SiteID") = objRS.Fields(0).Value
session("SiteTitle") = objRS.Fields(1).Value
session("SiteEmail") = objRS.Fields(2).Value
session("HostHeader") = objRS.Fields(3).Value
session("SiteDescription") = objRS.Fields(4).Value
session("sitehead") = objRS.Fields(5).Value
else
TempHost = 0
end if
objRS.Close
set objRS = nothing
conn.Close
set conn = nothing

HostLookUp = TempHost
End Function

Function RemoveWww(HostHeader)
RemoveWww = Replace(HostHeader,"www.","")
End Function

%>

viol
05-13-2002, 12:18 AM
Thanks Sanjay, but my host doesn't support ASP.
It supports php and perl (cgi and ssi).
Thanks again, anyway.

Website Rob
05-13-2002, 12:39 AM
It does bring up a good point though. When asking for help with "whatever" software, you do yourself a service by giving as much detail beforehand, as to what you are working with. Saves everybody a lot of time. ;)

So, with that said, I will presume you are on a Server that uses Apache and the following may or may not work for you:

Redirect 301 /dirname/oldpagel.html http://yourdomain.com/dir/newpage.html

This is a basic Apache command and needs to be put inside a file called .htaccess [note the period] which Windows will not let you create.


Confused? Don't be.

An htaccess file must be hidden in order for to work properly and be secure. Apache demands that hidden files always start with a period and Windows does not let files be created that start with a period. To solve this dilemma you first create a file just called "htaccess" -- no period no extension. Then upload it to your Hosting account in ASCII or Text file format. Place this file in your "root directory" -- same place you have your Home Page.

Once the file is on your Hosting Server you then use your Control Panel or an FTP program to rename the file starting with a period. If your Hoster allows ".htaccess" to work from within your Hosting account, everything will be fine. If it doesn't work you will need to ask them to allow ".htaccess" files for you.

m6.net
05-13-2002, 12:47 AM
Thanks Sanjay, but my host doesn't support ASP....

You are welcome. beleive still my post is worth... for other ASP users ;)

meballard
05-13-2002, 03:13 AM
Another way when using PHP scripts is just one command:
<? header("Location: http://newlocation..."); ?>
You could also setup the error document to be a PHP script that will look at the requested URL and redirect based on that using the above header (note there can't be any text passed to the browser before that command is sent).

bababooey
05-14-2002, 05:00 AM
You can also use JavaScript:

<script language=javascript>
window.location.href="http://www.yourdomainname.com/";
</script>

You could also use:

<script language=javascript>
window.location.replace("http://www.yourdomainname.com/");
</script>

The second script is better because it prevents the visitor from having to click the Back button twice if they want to go back to another page.

NodeHost
05-14-2002, 09:55 AM
The easiest way was already posted using .htaccess

Thank you for the other codes though, I have been looking for them :)

carolinahosting
05-14-2002, 11:28 AM
This may be an easier way... You could ask your hosting company to place a Standard Forward on your domain name.

IE: In stead of hosting your domain, they would forward the request from that domain to another domain.

Hope this helps...