.htaccess changes (optional)

Last updated on
21 March 2018

Drupal 7 will no longer be supported after January 5, 2025. Learn more and find resources for Drupal 7 sites

Default .htaccess file that comes with standard Drupal distribution handles www and non-www domains/subdomains for a specific domain. Below is the default .htaccess redirect that redirects www-to-non-www or non-www-to-www.

# To redirect all users to access the site WITH the 'www.' prefix,
  # (http://example.com/... will be redirected to http://www.example.com/...)
  # adapt and uncomment the following:
  # RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
  # RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
  #
  # To redirect all users to access the site WITHOUT the 'www.' prefix,
  # (http://www.example.com/... will be redirected to http://example.com/...)
  # uncomment and adapt the following:
  # RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
  # RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

Note that in above code, you have to replace example.com with your actual domain. If you have a multisite setup with multiple databases, you can add a few extra lines to perform www redirection for other domains. But with Domain Access, the approach is to handle multiple domains more easily. Rather than hardcoding the domains in the .htaccess file, it is better to do this globally using regular expressions.

You can force Domain Access module to process www subdomain of the base domain (www.example.com) or any other subdomain (www.en.example.com) as an alias of the parent domain (alias of example.com or an alias of en.example.com). See this page for the documentation on how to do this.

To implement domain independent redirects, you have to comment out any lines that you modified if you had www redirection set. Then, use suitable code from one of the below two options in between
# RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
and
# Modify the RewriteBase if you are using Drupal in a subdirectory or in a

Copy and paste one of the codes below depending on your need.

  • To redirect users to non-www version.
      # Redirect all users to access the site WITHOUT the 'www.' prefix
      RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
      RewriteCond %{HTTP_HOST} !\.([a-z-]+\.[a-z]{2,6})$ [NC]
      RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301]
    
  • To redirect users to www version.
      # Redirect all users to access the site WITH the 'www.' prefix
      RewriteCond %{HTTP_HOST} !^www\. [NC]
      RewriteCond %{HTTP_HOST} !\.([a-z-]+\.[a-z]{2,6})$ [NC]
      RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    

Help improve this page

Page status: No known problems

You can: