What alternative methods are there for redirecting all page requests to the www version of a domain?

I know how to eidt the root .htaccess file but find that this method does not survive core updates.

Is there another way? Perhaps with a module?


It's always going to be better to do this with .htaccess, or in other web server config, but if you need to do it in php, put this at the top of settings.php:

if ($_SERVER['HTTP_HOST'] == 'example.com') {
  header('Location:http://www.example.com' . $_SERVER['REQUEST_URI']);
  • This sounds like it would be a good method to use in an install profile or distribution Mar 8 '15 at 10:22
  • Server config will always be better @MrPaul (less overhead if php doesn't need to get involved in the redirect). The best solution is to put this stuff in the vhost - maximum performance, no worrying about updates. I don't personally think a distribution or install profile should make that sort of assumption about where the site is served from. This method should be considered a very last resort, for example if you're on a host that uses nginx, but doesn't let you edit the vhost, or a host that doesn't allow per folder overrides in Apache, and so on
    – Clive
    Mar 8 '15 at 10:53
  • 1
    My install profile idea would be for personal use only, but I take your point. Thanks Mar 8 '15 at 11:10

instead of looking any module, i advice to add www in your CNAME record at your domain control panel.

  • There is no mention of a control panel? Won't this just ensure that example.com and www.emample.com resolve to the same IP address? Or are you assuming that cPanel, Plesk, etc, will do the rewrite automagically?
    – mpdonadio
    Mar 8 '15 at 21:58

I have two domains one of which I was using for development and the other was functional. I simply put a redirection in Index.php to redirect for the one domain.

  • Modifying index.php or any core files is not a good idea. Your changes would be overwritten when Drupal is updated Mar 8 '15 at 21:58

You can change this behavior in your domain controller section of your cPanel

  • I don't see any mention of cPanel in the question, and where would this setting be?
    – mpdonadio
    Mar 8 '15 at 21:56
  • Where did you buy your host and domain? Depending on your cPanel, the section you want might appear in a different place. Regardless of the cPanel's look, there is a specific section just for redirection Mar 8 '15 at 22:06
  • if true, that would make the question and your answer off-topic here
    – Geoff
    Mar 8 '15 at 23:22
  • I don't think this is necessarily off topic as my question asks "is there another way?" Mar 9 '15 at 9:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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