Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Currently users can access the ourdomain.com BUT i want those visitors to be redirected to www.ourdomain.com

What is the best solution? - We have plesk, linux, apache server running php

Thanks

share|improve this question
2  
Why? no-www.org –  Jan Jungnickel Oct 9 '09 at 10:07
    
duplicate? serverfault.com/questions/66313/… –  warren Oct 9 '09 at 11:58
add comment

4 Answers 4

There are a couple of options:

if you have a seperate directory/section for ourdomain.com you can use an index.html to redirect your users

<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="REFRESH" content="0; URL=https//www.ourdomain.com">
<title>HTML REDIRECT</title>
</head>
<body>
</BODY>
</HTML>

you can also use a php program:

header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.yourdomain.com");
exit();

This has to be the first lines in your code to be part of the html header

share|improve this answer
add comment

What the best solution is you'll have to decide -- it does amongst other things come down to what technologies you're most able to maintain.

You can do the redirect via Apache's .htaccess, if this isn't disabled on your server (it is most often available, but sometimes its disabled for a slightly better performance / security). Here is a syntax example, and you could google htaccess for more.

You can also do the redirect via PHP. The syntax for the redirect itself would be something like:

header('HTTP/1.1 301 Moved Permanently');
header('Location: http://www.yoursite.com/');

Note that you'll need your PHP logic to discern between requests that come in with or without 'www.', and only send the header for those without 'www.'. If you're using some sort of 3rd party PHP CMS, then you probably shouldn't change its PHP code.

One last thing, if you care about search engine ranking, and you have previously had your site indexed both with and without 'www.', then you should fix that. In Google, log in to Google's Webmaster Tools, and set the canonical URL to the one you prefer, with or without www. For other search engines, inbound links etc, it could be beneficial to contact those who link to you, and have them update their links.

share|improve this answer
add comment

Not sure if you can, but perhaps mod_rewrite?

share|improve this answer
add comment

With mod_rewrite

RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.