Solved

Prepend www to URL using PHP

Posted on 2009-07-07
6
429 Views
Last Modified: 2013-12-13
I have a WordPress blog hosted on IIS and I've been trying to get ISAPI_Rewrite to work so that I can prepend www to the URL. It isn't working after 3 hours, so I just want to use PHP redirect to get it working. I've done it before with ASP (which I've included) but I don't know PHP well enough to translate this code. Can you do it?
<%

If InStr(Request.ServerVariables("SERVER_NAME"),"www") = 0 Then

	Response.Status="301 Moved Permanently"

	Response.AddHeader "Location","http://www."

		& Request.ServerVariables("HTTP_HOST")

		& Request.ServerVariables("REQUEST_URI")

	Response.End

End if

%>

Open in new window

0
Comment
Question by:drbill1
  • 4
6 Comments
 
LVL 9

Expert Comment

by:xBellox
ID: 24797735
something like this:

<?

if (strpos($_SERVER['SERVER_NAME'], "www")===false)

    header("Status: 301")

    header("Location: http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']");

}

?>

Open in new window

0
 
LVL 9

Expert Comment

by:xBellox
ID: 24797790
I forgotthe ;  at the end of header("Status: 301") and "exit;" in place of  "Response.End"

... The correct should be:

<?

if (strpos($_SERVER['SERVER_NAME'], "www")===false)

    header("Status: 301");

    header("Location: http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']");

    exit;

}

?>
 

or 
 

<?

if (strpos($_SERVER['SERVER_NAME'], "www")===false)

    header("HTTP/1.1 301 Moved Permanently");

    header("Location: http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']");

    exit;

}

?>

Open in new window

0
 
LVL 9

Accepted Solution

by:
xBellox earned 400 total points
ID: 24797837
Oops, I did it again.. A little typo at the end of line ...($_SERVER['REQUEST_URI']") that  double quote doesn't exists.

I really think we should be able to delete ours comments.

Anyway, here is the correct code (hopefully :))

<?

if (strpos($_SERVER['SERVER_NAME'], "www")===false)

    header("Status: 301");

    header("Location: http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);

    exit;

}

?>

 

or 

 

<?

if (strpos($_SERVER['SERVER_NAME'], "www")===false)

    header("HTTP/1.1 301 Moved Permanently");

    header("Location: http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);

    exit;

}

?>

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 10

Assisted Solution

by:Chad Smith
Chad Smith earned 100 total points
ID: 24797839
you have forgotten the open bracket { and the www in your  Location:
<?

if(strpos($_SERVER['SERVER_NAME'], "www") === false) 

{

	header("HTTP/1.1 301 Moved Permanently");

	header("Location: http://www." . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);

	exit;

}

?>

Open in new window

0
 
LVL 9

Expert Comment

by:xBellox
ID: 24797931
Yeah, 3 little typos in just one question... Might be the record :P

Thanks for the help :)
0
 

Author Comment

by:drbill1
ID: 24798633
Worked perfect with the corrected typos, thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Sort Multidimensional Array 11 34
Update database from a form 8 35
wamp versus xampp 4 43
PHP: concatenate query 12 33
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now