Forum Moderators: coopster

Message Too Old, No Replies

Conditional redirect with php

Need some help

         

Marshall

5:11 pm on Mar 19, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



First to admit that I do not know php well at all. I know how to do this in asp:

<%
Select Case Request.ServerVariables("HTTP_HOST")
Case "example.co", "www.example.co"
Response.Redirect "http://www.example.com/index.asp"
End Select
%>

but have no clue how to write it in php for placement in the <head>.

Any help would be appreciated.

Marshall

eelixduppy

6:50 pm on Mar 19, 2012 (gmt 0)



Something like this?


if($_SERVER['HTTP_HOST'] == 'example.co' || $_SERVER['HTTP_HOST'] == 'www.example.co') {
header("Location: http://www.example.com/index.asp");
exit;
}

Marshall

7:09 pm on Mar 19, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Exactly like that. Thanks

Marshall

g1smd

8:46 pm on Mar 19, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The above code is for a 302 redirect. Add a preceding HEADER directive to set it as a 301 redirect.

The example also redirects to a named index file. You should redirect to bare folder ending with a trailing slash. The DirectoryIndex directive will take care of the rest.

rocknbil

4:19 pm on Mar 20, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This should really be at the server level and not in programming. Is it still a Windows server, and at least IIS 7?

Play with this [learn.iis.net], what you want is a slight modification of a canonical rewrite. It took M.S. forever to finally come around to the importance of rewrites and add a compatible module into their server software, but it works similarly to mod_rewrite.