1

How can I redirect all traffic from www.domain.com/page to domain.com/page?

I need to disallow traffic to web addresses that include the www. The reason is that I have an SSL certificate that only works with non-www web addresses. If users try to access https://www.domain.com, they get a certificate error.

3
  • 1
    Do you need this to be protocol aware (https and http) or just work for http?
    – mpdonadio
    Commented Sep 10, 2013 at 20:40
  • it's not protocol aware, I have a Drupal module that takes care of that, it redirects to https when it's an webpage that requires authentication
    – leonel
    Commented Sep 11, 2013 at 15:00
  • Please check: How to redirect non-www to www url in drupal.
    – kenorb
    Commented Aug 5, 2014 at 15:08

2 Answers 2

6

Uncomment lines 93 and 94 in the .htaccess file that comes with Drupal in the web root.

RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewiteRule ^ http://%1%{REQUEST_URI} [L,R=301]

EDIT: If the certificate requires https, edit the second line to read:

RewiteRule ^ https://%1%{REQUEST_URI} [L,R=301]
1

This is how you can achieve that in Drupal 7, editing your .htaccess

# 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} .
# RewriteCond %{HTTP_HOST} !^www\. [NC]
# RewriteRule ^ http%{ENV:protossl}://www.%{HTTP_HOST}%{REQUEST_URI} [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 the following:
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http%{ENV:protossl}://%1%{REQUEST_URI} [L,R=301]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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