Hello Everyone,

I have just been reading up on seo and drupal and it was recommended to redirect non-www to www or vice-versa. Just don't keep both.

I am a little confused by the default .htaccess file as it seems to have some differing characters in the relevant lines in the file as compared to other examples I found on google.

Also, this particular site is a .co.uk so here's what I have in the file if I follow the drupal comments:

Lets' assume the website is called: www.joebloggs.co.uk

Here's the original section in the .htaccess file after the drupal install:

# 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]
#

Here is my amended section in the .htaccess file:

# 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} ^joebloggs\.co.uk$ [NC]
RewriteRule ^(.*)$ http://www.joebloggs.co.uk/$1 [L,R=301]
#

What is confusing me is that in the first line there is a back slash between the joebloggs and the .co.uk

After doing some googling, none of the other examples I found had this back slash between the joebloggs and the .co.uk in the first line.

I am a complete beginner regarding .htaccess files and I want to make sure I have done the correct thing. I would kick myself I I got such an important thing wrong.

Please help.

If it is correct, a brief clarification what this back slash means, would be fantastic.

Thank you,
Sam

Comments

Mark Theunissen’s picture

You should have the following:

^joebloggs\.co\.uk$

The backslash is an escape character in the regular expression. Without the slash, the dot will match any character, so I guess technically it will still work, but for the sake of being correct, put in the \. which only matches a dot....

http://en.wikipedia.org/wiki/Regular_expression

Hope that helps!

Mark Theunissen’s picture

p.s. just so you know, the ^ matches the beginning, and the $ matches the end of the string.

Steady’s picture

Mark,

Thank you for your very swift and extremely clear reply!!!

My headache has gone :-)

Cheers,
Sam

Mark Theunissen’s picture

No problem ;)

BassPlaya’s picture

basically, if a user types in http://joebloggs.co.uk then he doesn't find the page while if you type joebloggs.co.uk he changes to the correct path..

I tried the following below but it doesn't work:

  RewriteCond %{HTTP_HOST} ^http\:\/\/joebloggs\.co\.uk$ [NC]
  RewriteRule ^(.*)$ http://www.joebloggs.co.uk/$1 [L,R=301]
  RewriteCond %{HTTP_HOST} ^joebloggs\.co\.uk$ [NC]
  RewriteRule ^(.*)$ http://www.joebloggs.co.uk/$1 [L,R=301]

any suggestions on how to do this?

Authentically,
BassPlaya

lpalgarvio’s picture

hey
here's some .htaccess code

redirect domain.tld to www.domain.tld, but not other subdomains (like site2.domain.tld):

  # 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]

redirect www.domain.tld to domain.tld, but not other subdomains (like site2.domain.tld):

  # 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]

based on D7 .htaccess with inclusion of no other subdomains condition

Luís Pedro Algarvio
Site Reliability / DevOps Engineer.
Free and Open Source Software advocate.
Passionate about life and work.
lp.algarvio.org

BeaPower’s picture

THanks this worked!

selva8187’s picture

Thanks it's worked for me

DewanCodes’s picture

# adapt and uncomment the following:

RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

Replace your domain with "example.com" above.

TechyTechy’s picture

Just use the below code. You can even redirect all users to HTTPS. Simply replace http with https.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^abc.com$
RewriteRule (.*) http://www.abc.com/$1 [R=301,L]

Check here for more conditions.