1

My website is currently accessible via 2 address

www.mydomain.com and mydomain.com

What is the best way to manage redirects. I would like

mydomain/blah to redirect to www.mydomain.com/blah

3

I always use the vhost file for this:

# Enforce domain.
RewriteCond %{HTTP_HOST} !^example.com(:.*)?$
RewriteCond %{HTTP_HOST} ^(?:.*?)(:.*)?$

RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

I would much rather to this on the Apache level, than PHP, to keep it as fast a possible.

Similar configuration for nginx here.

  • 1
    The .htaccess file that comes with Drupal has already a rewrite rule for that. – kiamlaluno Sep 3 '12 at 22:15
  • Good point. I keep the rule in a separate vhost file though so I can easily version control it. – Letharion Feb 1 '13 at 10:33
3

You can open up the .htaccess file that's in the root directory of your Drupal install and uncomment the last two lines of this code snippet starting with RewriteCond and RewriteRule.

  # If your site can be accessed both with and without the 'www.' prefix, you
  # can use one of the following settings to redirect users to your preferred
  # URL, either WITH or WITHOUT the 'www.' prefix. Choose ONLY one option:
  #
  # To redirect all users to access the site WITH the 'www.' prefix,
  # (http://example.com/... will be redirected to http://www.example.com/...)
  # uncomment the following:
  # RewriteCond %{HTTP_HOST} !^www\. [NC]
  # RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Right after this snippet in the .htaccess file, there's another couple of lines to uncomment if you wanted www.example.com to redirect to example.com.

1

Looks at a redirect module like 301 redirect, I have not tried it but the description seems to cover what you require.

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.